Coins not adding

Hello! I am making a coin system and it adds the coins when the players leaderstats changes. It recognizes when the leaderstat changes using a server script firing a remote event, and the local script in startergui picks it up, and it prints "set coins".

However, it doesn’t seem to be adding the coins. I don’t know why. No errors in the output and it goes through the script with no problems. Thanks for your help.

--server script in SSS
game.Players.PlayerAdded:Connect(function(plr)
	plr:WaitForChild("leaderstats"):WaitForChild("Stage"):GetPropertyChangedSignal("Value"):Connect(function()
		print('player leaderstats changed')
		local leaderstats = plr:WaitForChild("leaderstats"):WaitForChild("Stage").Value
		local coinEvent = game.ReplicatedStorage.CoinEvent
		print(leaderstats)
		--task.wait(0.001)
		print('Help me')
		if leaderstats < 20 and leaderstats > 1 then --in easy
			coinEvent:FireClient(plr, 2) --add 2 coins
			print('fired server 2 coins')
		elseif leaderstats < 40 and leaderstats > 20 then --if in casual
			coinEvent:FireClient(plr, 4)
		elseif leaderstats < 51 and leaderstats > 40 then --if in medium
			coinEvent:FireClient(plr, 5)
		end
	end)
end)

--local script in startergui
local coinReceiver = game.ReplicatedStorage.CoinEvent
print('set localers')

coinReceiver.OnClientEvent:Connect(function(coinValue)
	print('received')
	local coinText = script.Parent.Text
	local coinVal = tonumber(coinText)
	coinText = tostring(coinVal + coinValue)
	print('set coins')
end)

No errors in the server script either.

Replace this:

with this:

local coinText = script.Parent
local coinVal = tonumber(coinText)
coinText.Text = tostring(coinVal + coinValue)

The problem with your code is that you can’t update the value of the Text property just by changing a variable.

1 Like

Why not handle all of this on the client? You are only listening to when a IntValue is updated then updating a GuiObject respectively, and a RemoteEvent is unnecessary. Note that updating the IntValue itself should be done on the server though.

I am going to add a data store to this and the local script way would be much more confusing.

The IntValue is already replicated to the client when you update it on the server, so its easiest to listen to the value being changed on the client and update the GuiObject accordingly.

image

Change this:

local coinVal = tonumber(coinText)

to this:

local coinVal = tonumber(coinText.Text) or 0
1 Like

It works now, however it stays at 2 coins even when I change the stages value leaderstats.

It looks like the issue might be in your local script. In the coinReceiver.OnClientEvent function, you are updating the coinText variable but not actually updating the value of the Text property of the GUI element that displays the coins.

To fix this, you should update the Text property of the GUI element with the new value of coinText after updating it:

local coinText = script.Parent.Text
local coinVal = tonumber(coinText)
coinText = tostring(coinVal + coinValue)
script.Parent.Text = coinText  -- Add this line to update the GUI element with the new coin value
print('set coins')

Without this update, the value of coinText is being updated, but it’s not being reflected in the game’s UI.

2 Likes

Hers is different because she is doingscript.Parent.Text = script.Parent.Text. I don’t know how that changes anything but it works now lol. I tried your way but it didn’t work so.

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