How to change the value of a TextLabel depending on the game status

Hey there, so what I am trying to do is to have a TextLabel that changes depending on the stage of the game or if the players are playing

I have this coin Gui: image

I want the player to be able to grab a max amount of 25 coins per game, think of a sort of minigame type of system

I want it to be this when the game starts: https://gyazo.com/2ba9b2e23a13fd985bb8b01d9cc3ca23
In this GIF you can see it changes, but this is not refreshing, if I grab a coin it wont show up

And at the end of the game when the coundown gets to 0 I want it to go back to the normal coins, making sure all the time it is in a loop to check if the player grabbed a coin, spent or if the value changed

What I have so far and that does not work is this:

In the minigameHandler

if i == 0 then -- If the game started
 
    displayManager.updateStatus("GO!")						 
    displayManager.updateCoinStatus(player.leaderstats.InGameCoins.Value .. " / " .. player.leaderstats.MaxCoins.Value, player)

On the moduleScript

function DisplayManager.updateCoinStatus(newStatus,Player)
	Player.PlayerGui.Coins.Gradient.Coins.Text = newStatus	
end

On a localScript inside the coinText

local textLabel = script.Parent

local function updateText()
	
	if status.Value == "GO!" then
		
			textLabel.Text =player.leaderstats.InGameCoins.Value .. " / " .. player.leaderstats.MaxCoins.Value
		
	end
	if status.Value == "Game ended!" then
		
		textLabel.Text = player.leaderstats.Coins.Value
	
	end
	
	
end

status.Changed:Connect(updateText)
updateText()

Any help is really appreciated!

1 Like

Maybe I’m wrong or maybe I’m right, but I don’t see a code that updates the gui depending on how many coins the player has collected. I do see the module script, but it would only run once. With the changed function, it would run everytime the value has changed, or infinitely. You should add a function that detects everytime the “InGameCoins” value changes. When that happens, we update the coin value.

Also, since you wanted the coin value to change when the game ends, you can call the function updateText() and disconnect the .Changed event.

1 Like

You were right with the first part, I added this function to check all the time if the value changes and works

local function updateCoins()
	
	textLabel.Text = InGameCoins.Value .. " / " .. player.leaderstats.MaxCoins.Value
	
end	
	
InGameCoins.Changed:Connect(updateCoins)

Now could you explain to me how to do the part when the game ends? I don’t really know how to approach it

1 Like

well when the game ends, you can disconnect the .Changed function.

heres how you disconnect functions:

local connection
connection = part.Touched:Connect(function()
 if thing == "End" then
  connection:Disconnect()
 end
end)

After you disconnect, you can also run the updateText() function to restart the textlabel text.

Sorry for the late reply

1 Like

I am getting an error when disconnecting

image

if status.Value == "Game ended!" then
		InGameCoins.Changed:Disconnect()
		textLabel.Text = player.leaderstats.Coins.Value
		
	end
InGameCoins.Changed:Connect(updateCoins)

you have to create the connection for the function first,

local connection
connection = InGameCoins.Changed:Connect(updateCoins)

also remember to do connection:Disconnect() in the end.

Finally, you might want to make it an old fashioned connect function so connection is a variable. For example:

 local connection
 connection = InGameCoins.Changed:Connect(function(updateCoins)

....

connection:Disconnect()
end)

Nevermind, it is working perfectly! thank you very much!

1 Like