CoinValue not adding

Hello, I am making a coin system where every time you complete a stage, you gain 2 coins. However, it only works one time. Whenever I complete a stage it gives 2 coins, but when I complete another stage it doesn’t give the coins but it says it does.

Can anyone help? Thanks, here is my code:

local plr = game.Players.LocalPlayer
local coinText = plr.PlayerGui.CoinSystem.Frame.TextLabel
local coinValue = tonumber(plr.PlayerGui.CoinSystem.Frame.TextLabel.Text)
local leaderstats = plr.leaderstats.Stage.Value
--local stageIn = 0
print('set locales')

for _, v in pairs(workspace.Checkpoints:GetChildren()) do
	v.Touched:Connect(function(hit)
		if hit.Parent == plr.Character then
			local stageNum = tonumber(v.Name)
			print(plr.leaderstats.Stage.Value)
			if stageNum < leaderstats or stageNum > leaderstats then
				warn('person is less or more than their stage number')
				return
			else
				print('Help me')
				--if stageIn == 0 then
				if stageNum < 20 and stageNum > 1 then --in easy
					local newCoin = coinValue + 2
					coinText.Text = tostring(newCoin) --add 2 coins
					print('added easy coins')
					--stageIn = stageNum
				elseif stageNum < 40 and stageNum > 20 then --if in casual
					coinText.Text = tostring(coinValue + 4)
					--stageIn = stageNum
				elseif stageNum < 51 and stageNum > 40 then --if in medium
					coinText.Text = tostring(coinValue + 5)
					--stageIn = stageNum
				end
				--elseif stageIn == stageNum then
				--	print('stagein is equal to stagenum')
				--	print(stageIn, stageNum.. "stageIn, stageNum")
				--	return
				--end
			end
		end
	end)
end

No errors in the output, it prints “added easy coins”, but yet the coin value still stays at 2.

coinValue is set once and then never changes. Each time you want increment coins you need to read in the existing value again.

It would be cleaner to separate the data and the textlabel too but not strictly necessary. E.g. have a variable for your coins which is always a number.

1 Like

It works!! Thank you!

I have one problem though, it keeps adding coins if I walk over the checkpoint. Is this possible to fix?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.