[This post is old, nothing here reflects the way i program and make posts today]
I want a system in which the player earns coins and instead of updating everything at once, updating one at a time, but I tried several times, looked at the devhub, researched various solutions and nothinga
The problem is that CoinsToAdd (NumberValue) does not update, and always gives as 0 after currency gain
The script does not keep track of the coins, as the coin gain increases, it stores all earnings, and then updates, but that is not what happens.
Script is a LocalScript located inside TextLabel
Script I wrote:
local player = game.Players.LocalPlayer
local Coins = player.leaderstats.Coins
script.Parent.Text = Coins.Value
local function Add()
local GC = script.CoinsToAdd
for ToAdd = 1, GC.Value do
print("started")
GC.Value = GC.Value -1
script.Parent.Text = script.Parent.Text +1
wait(0.5)
end
end
Coins:GetPropertyChangedSignal("Value"):Connect(function()
local oldMoney = Coins.Value
local gived = Coins.Value -oldMoney
local GC = script.CoinsToAdd
if GC.Value >0 then
Add()
else
GC.Value = 1
Add()
wait(1)
GC.Value = GC.Value -1
end
end)
You don’t need to write the whole code for me if you don’t want to, just give tips, and if this post is in the wrong category, let me know
your missing a small piece on your “for” loop
“for” loop need three Arguments
Variable, Starting number and how much to add each time the loop runs
Oh i see what happening I have to do something right now so ill just give you the fixed code
local GC = script.CoinsToAdd -- In future place your variables here just not ones that have stuff like .Value and such
local Coins = player.leaderstats.Coins
script.Parent.Text = Coins.Value
local function Add()
local GC = script.CoinsToAdd
for ToAdd = 1, GC.Value do
print("started")
GC.Value = GC.Value -1
script.Parent.Text = script.Parent.Text +1
wait(0.5)
end
end
Coins:GetPropertyChangedSignal("Value"):Connect(function()
local oldMoney = Coins.Value
local gived = Coins.Value - oldMoney
--local GC = script.CoinsToAdd the "Add" function can't see this variable
if GC.Value >0 then
Add()
else
GC.Value = 1
Add()
wait(1)
GC.Value = GC.Value -1
end
end)
So do you get any output at all? Put a print statement in the property changed signal and see if it is updating. Add it in multiple locations. Also, try adding breakpoints and see what the value of GC.Value is at multiple points.