Why is the text not changing?

Ok so please don’t make fun of what I can do, I started a couple weeks ago. I was trying to make a script to give a player “Bucks”. That works, but I tried making a gui that shows the amount of money someone has, but that’s now working.

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

	local Bucks = Instance.new("IntValue")
	Bucks.Name = "Bucks"
	Bucks.Parent = leaderstats

	local Time = Instance.new("IntValue")
	Time.Name = "Time"
	Time.Parent = leaderstats

	while wait(5) do
		Bucks.Value = Bucks.Value + 5
	end
	local BucksDisplay = game.StarterGui.ScreenGui.TextButton.TextLabel
	BucksDisplay.Text = Bucks.Value
	
end)

What’s wrong with this?

while wait(5) do
	Bucks.Value = Bucks.Value + 5
end

will just keep looping non-stop, which means the codes after it will not run. You can try to wrap it with spawn(function()

spawn(function()
	while wait(5) do
		Bucks.Value = Bucks.Value + 5
	end
end)
1 Like

First off the while loop is looping and will never reach to what is below it. So you would either need to change the Gui text with a .Changed event or directly in the loop. Second, you must change the text on the client preferably and reference the Gui from the player’s PlayerGui.

Here’s a good version of it

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

	local Bucks = Instance.new("IntValue")
	Bucks.Name = "Bucks"
	Bucks.Parent = leaderstats

	local Time = Instance.new("IntValue")
	Time.Name = "Time"
	Time.Parent = leaderstats

    coroutine.wrap(function() -- Creating the thread with this function
    	while true do
            wait(5) -- prefer calling wait(5) here then in the condition of the loop
		    Bucks.Value = Bucks.Value + 5
	    end
    end)() -- calling the thread

    Bucks.Changed:Connect(function() -- Detecting everytime it changes
    	    local BucksDisplay = player.PlayerGui.ScreenGui.TextButton.TextLabel
	    BucksDisplay.Text = Bucks.Value
	end)
end)
2 Likes

It started out with label, now it says 0, but it doesn’t update

1 Like

I noticed that you’re trying to update the TextLabel in StarterGui:

local BucksDisplay = game.StarterGui.ScreenGui.TextButton.TextLabel

you need to update it in PlayerGui

Thanks to both of you for helping me