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

Updating Cash GUI script not updating when leaderstats value changes?

Asked by 4 years ago

So i made a leaderstats script and a gui that shows your leaderstats value, but for some reason it dosent update and i dont know why, here is the leaderstats script

game.Players.PlayerAdded:connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local money  = Instance.new("IntValue")
money.Name = "Cash"
money.Parent = leaderstats
money.Value = 0

local crops  = Instance.new("IntValue")
crops.Name = "Crops"
crops.Parent = leaderstats
crops.Value = 100

local level  = Instance.new("IntValue")
level.Name = "Level"
level.Parent = leaderstats
level.Value = 0

end)

and here is my gui script

local player = game:GetService("Players").LocalPlayer
local cash = player.leaderstats.Cash.Value
  script.Parent.Text = math.floor(cash)

    cash.Changed:Connect(function()
        script.Parent.Text = math.floor(cash)
    end)






1 answer

Log in to vote
0
Answered by 4 years ago

Problems:

  • You put a .Value after getting the cash inside leaderstats.

  • Text requires a string, not a number.


Solutions:

  • Remove the .Value after defining the player's cash as Changed is an event of an instance, not a number (IntValue's value).

  • Use the tostring() function that converts a number into a string.


Recommendations:

  • Use WaitForChild() to make sure the leaderstats and cash have been created by the other script.

  • Remove math.floor() as you do not need to use them since IntValue's automatically round numbers to the nearest whole number.


Fixed Script:

local player = game:GetService("Players").LocalPlayer
local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
script.Parent.Text = tostring(cash.Value)

cash.Changed:Connect(function()
    script.Parent.Text = tostring(cash.Value)
end)






Ad

Answer this question