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
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
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
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
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