user-interfacetextluaroblox

How to update/refresh UI in a script?


I'm working on creating a player GUI in ROBLOX. The GUI is supposed to display the player's money, sadly when I test it and try to update the player's coins, the GUI doesn't update with it. I added an old code because it worked in an old project that I was working on in a different language. I didn't really know what I was thinking when I added that.

Here's the code:

local player = game.Players.LocalPlayer
coinAmount = player:WaitForChild("leaderstats"):WaitForChild("Coins")
oldCoinAmount = player:WaitForChild("leaderstats"):WaitForChild("Coins")
text = script.Parent

if coinAmount ~= oldCoinAmount then
    text = oldCoinAmount
end

I was thinking maybe there was a way to constantly refresh the script, I don't know how though.

Also, this LocalScript is a child of the actual GUI It also doesn't throw any errors.


Solution

  • I figured it out, also if you trying to do this you can use this script:

    local player = game.Players.LocalPlayer
    coinAmount = player:WaitForChild("leaderstats"):WaitForChild("Coins")
    text = script.Parent
    
    coinAmount.Changed:connect(function()
        text.Text = coinAmount.Value .. " Coins"
    end)