Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

lua - attempt to concatenate nil with string Roblox

I'm very confused. I'm trying to make it so that if you hold E with Roblox's "ProximityPrompt", you will get a GUI on your screen with some text. Everything works except that the text won't work. I also am not writing a string on the client script. There is a variable for it on the server script that gets passed over. But I keep seeing this error in the output.

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

Here is what I'm doing in my script

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)

And in the local script that listens for 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) 

Please help, that would be greatly appreciated.

question from:https://stackoverflow.com/questions/65838435/attempt-to-concatenate-nil-with-string-roblox

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your error is telling you that the object you are trying to add to a string is undefined. This could be item.Name or price.Value is undefined and causing this string construction to fail.

Looking at how you are defining item and price shows that both of these values are undefined in your LocalScript's callback. When you call a RemoteEvent's FireClient function, the first argument tells the engine who to send the event to, and all the other arguments get passed in as the arguments of the callback. Currently, you aren't passing in any arguments at all.

So to fix your problem, you need to pass in the right arguments from the Script:

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

and parse them properly in your 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) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...