Roblox Studio "attempt to concatenate nil with string"

当我在 Roblox Studio 中,我像您之前的示例一样编写了一个脚本:

local amount = 123

local Module = require(game.ServerScriptService:WaitForChild("Module"))

script.Parent.ClickDetector.MouseClick:Connect(function(player)

    if player.leaderstats.Currency.Value >= amount then

        player.leaderstats.Currency.Value = player.leaderstats.Currency.Value - amount

        local pet = Module.chooseRandomPet()

        --> print(pet.Name.." selected") <--

    end

end)

当我要使用它时,我被“尝试将 nil 与字符串连接”所困扰,箭头所在的区域。

我该如何解决这个问题。

点赞
用户2858170
用户2858170

Module.chooseRandomPet() 返回一个没有 "Name" 字段的表。我猜你得到了一个简单的 Lua 表,而你期望得到的是一个 Roblox 实例。或者你因为某些原因把该实例的 Name 属性赋值为 nil。

无论如何,你应该弄清楚为什么没有得到你期望的结果。我在网上找到了一些宠物模块的问题。所有这些问题都有很多共同的代码错误。

如果你不能确定你是否得到了你期望的结果,那么至少应该通过检查要连接的是否为 nil 值,正确处理该情况。

2021-04-08 15:08:46