尝试将 nil 与字符串 Roblox 连接起来

attempt to concatenate nil with string Roblox

我很困惑。我试图做到这一点,如果您在使用 Roblox 的“ProximityPrompt”时按住 E,您将在屏幕上看到一个带有一些文本的 GUI。一切正常,除了文本不起作用。我也没有在客户端脚本上写字符串。在传递过来的服务器脚本上有一个变量。但是我一直在输出中看到这个错误。

Players.ford200000.PlayerGui.BuyGui.Frame.TextInput.Text:2: attempt to concatenate nil with string - Client -

这是我在脚本中所做的事情

local sp = script.Parent
sp.ProximityPrompt.Triggered:Connect(function(player)
    local name = sp.Name
    local ss = game.ServerStorage
    local item = ss.Hats:FindFirstChild(name)
    local price = item.Price
    game.ReplicatedStorage.ShopClickEvent:FireClient(player)
    game.ReplicatedStorage.ShopInfoEvent:FireClient(player)
end)

并在侦听 ShopInfoEvent 的本地脚本中

game.ReplicatedStorage.ShopInfoEvent.OnClientEvent:Connect(function(player, price, item)
    script.Parent.Text = "Would you like to buy this ".. item.Name .." for ".. price.Value .."?"
end) 

请帮忙,将不胜感激。

您的错误告诉您您尝试添加到字符串的对象未定义。 这可能是 item.Nameprice.Value 未定义并导致此字符串构造失败。

查看您定义 itemprice 的方式表明这两个值在您的 LocalScript 回调中均未定义。当您调用 RemoteEvent's FireClient function 时,第一个参数告诉引擎将事件发送给谁,所有其他参数都作为回调的参数传入。目前,您根本没有传递任何参数。

因此,要解决您的问题,您需要从脚本中传递正确的参数:

game.ReplicatedStorage.ShopInfoEvent:FireClient(player, price, item)

并在您的 LocalScript 中正确解析它们:

game.ReplicatedStorage.ShopInfoEvent.OnClientEvent:Connect(function(price, item)
    script.Parent.Text = "Would you like to buy this ".. item.Name .." for ".. tostring(price.Value) .."?"
end) 

Players.ford200000.PlayerGui.BuyGui.Frame.TextInput.Text:2: attempt to concatenate nil with string - Client -

告诉你你需要知道的一切。

您正在尝试将 nil 与字符串连接起来。这意味着您在第 2

行中使用字符串连接运算符 .. 和 nil 操作数

所以让我们看一下第 2 行

script.Parent.Text = "Would you like to buy this ".. item.Name .." for ".. tostring(price.Value) .."?"

"Would you like to buy this "" for ""?" 显然是字符串。剩下 item.Nametostring(price.Value).

如果 price.Valueniltostring 会变成 "nil"。所以这不可能是这个特定错误消息的原因。

剩下 item.Name。如果 item 是 nil,我们会看到一个错误,因为它索引了一个 nil 值。所以这告诉我们,无论 item 是什么,它都不是我们所期望的。一个 table 包含键“Name”的元素。

这时候你就知道你的函数的参数有问题了。所以你(希望再次)参考手册并将其与你使用这些事件函数的方式进行比较。