How can i make a coin counter

i want to have a gui saying how many coins have been collected i already got a coin working
the amount of coins is stored in an int value which ive tested and it does go up if you get a coin

1 Like

You can simply have a localscript in a textlabel that detects when the coin intvalue has changes and update it accordingly.

Example, let’s say your Intvalue is called “Coins” and is located in your player, you can simply do this in a localscript in your TextLabel

local Players = game:GetService("Players")

local player = Players.LocalPlayer
local coins = player:WaitForChild("Coins")

local label = script.Parent

coins.Changed:Connect(function(newval)
	label.Text = tostring(newval)
end)

And it should work, if it’s located somewhere else, just change the code around to reference with it is located, and ensure it is somewhere the clients can see, example, if you put it in ServerStorage, the clients can’t see it so it wont work if you try to reference it

3 Likes

how could i add text to the front (example: "Coins: ")

Simply just concatenate the value with a string, so

label.Text = tostring(newval)

Would be

label.Text = "Coins: " .. tostring(newval)

For your usecase. If the new value was 2, it would display Coins: 2, because it combines Coins: with the current value

Assuming Coins’s value is an int you don’t have to tostring but yeah :man_shrugging:

I was still skeptical about it so I used tostring to ensure it will work for what @OP requires, but if that’s fine

@Bedu009 You can also just do this

label.Text = "Coins: " .. newval

If concatenation works with numbers as well, but again, better to be safe and use tostring

didnt work the code i have so far is

local label = script.Parent

local coins = game.Workspace.Coins

coins.Changed:Connect(function(newval)

label.Text = "Coins: " … tostring(newval)

end)

Any errors? Where is the script located in the explorer? If you’re referring to taht it wont work the first time, you can simply give it a default value. So before the changed event, you can do

label.Text = "Coins :" .. tostring(coins.Value)

W001: (4,36) Unknown global ‘newval’
and i added the label.Text = “Coins :” … tostring(coins.Value) u wanted me to add and the text changed from "Coins: " to “Coins: nil”

the script is in the textlabel

Okay show me your explorer where the localscript is located and the code you have currently, I think it may be an issue there

wait its suppose to be a localscript? oops

I think it could be that, but the error you’re having must be a mistake you put in the code, may I see the full code you have currently?, remember to format your code using 3 backticks ``` at the start and end of the code block

image


local label = script.Parent
local coins = game.Workspace.Coins
label.Text = "Coins: " .. tostring(newval)
coins.Changed:Connect(function(newval)
	label.Text = "Coins: " .. tostring(newval)
end)

that causes “Coins: nil”


local label = script.Parent
local coins = game.Workspace.Coins

coins.Changed:Connect(function(newval)
	label.Text = "Coins: " .. tostring(newval)
end)

causes "Coins: "

Your code should be this

local label = script.Parent
local coins = game.Workspace.Coins

label.Text = "Coins: " .. tostring(coins.Value)

coins.Changed:Connect(function(newval)
	label.Text = "Coins: " .. tostring(newval)
end)

newval is only local to the event, if it doesn’t work whe nthe value is changed, then you can scrap using newval and just always use coins.Value

i got “Coins: 0” but when i collected the coin it didnt update “yes i can confirm it updates”

Maybe try adding some prints to see what’s really happening

Try scrapping newval and adding a print statement

local label = script.Parent
local coins = game.Workspace.Coins

label.Text = "Coins: " .. tostring(coins.Value)

coins.Changed:Connect(function()
	print("Change detected")
	label.Text = "Coins: " .. tostring(coins.Value)
end)

If it doesn’t work, then it may be an issue with how the value is being updated

the coin reports the value has been updated (and looking at the value it had been updated) but the counter didnt print anything

So it’s something going on with the detection, does the value change when checking from the client?

Maybe try this?

local label = script.Parent
local coins = workspace.Coins

label.Text = "Coins: " .. tostring(coins.Value)

coins:GetPropertyChangedSignal("Value"):Connect(function()
	print("Change detected")
	label.Text = "Coins: " .. tostring(coins.Value)
end)

If this doesn’t work either, it’s something wrong with the way it’s being incremented or the client can’t see the changes