Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

attempt to concatenate field 'Gold' (a userdata value)?

Asked by 5 years ago
  local label = script.Parent --varaible for less typing
while true do --changed to while statement
    label.Text =  ('Gold:' .. game.Players.LocalPlayer.leaderstats.Gold)
    print("Working!")
end

i am currently working on a GUI that types the amount of Gold the player has. Any ideas?

3 answers

Log in to vote
0
Answered by 5 years ago

You need to access the .Value of Gold, and convert it to a string. Your code should look like:

  local label = script.Parent --varaible for less typing
while wait() do --changed to while statement(less laagy)
    label.Text =  ('Gold:' .. tostring(game.Players.LocalPlayer.leaderstats.Gold.Value))
    print("Working!")
end
0
Thank you :D mixgingengerina10 223 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

You are concatenating a string with an object, while strings can only be concatenated with strings. Here's the fixed version:

  local label = script.Parent --varaible for less typing
while true do --changed to while statement
    label.Text =  ('Gold:' .. game.Players.LocalPlayer.leaderstats.Gold.Value)
    print("Working!")
end

Basically, what I mean with what I just said before, is that you can do this:

E = 'Test'
print( E .. 'Double' ) -- prints out TestDouble 

but not this:

E = 'Test'
print( E .. workspace.Double ) -- Errors
0
Thank you for trying :D i liked your idea there, now I understand how I can make things better, thanks for putting your effort into this :D mixgingengerina10 223 — 5y
Log in to vote
0
Answered by 5 years ago

This is because you tried to concatenate an object, and wrapped your string in brackets. Brackets are math exclusive. Instead of using a loop to update the text, use the Changed event to update it. The loop isn’t fast enough to get it updated.

local plr = game:GetService('Players').LocalPlayer
local stats = plr:WaitForChild'leaderstats'
local gold = stats.Gold

gold.Changed:Connect(function() --  use this instead of loop 
    script.Parent.Text = "Gold: "..gold.Value -- don’t wrap your string in brackets () since brackets are math exclusive. 
end)
0
Thank you for trying :D i liked your idea there, thanks for putting your effort into this :D mixgingengerina10 223 — 5y
0
"don’t wrap your string in brackets () since brackets are math exclusive." - incorrect. Both `print("foo" .. ("bar"))` and `print(("foo" .. "bar"))` are valid. fredfishy 833 — 5y

Answer this question