Help with leaderstats increasing after decreasing

Hello everyone,

I am having trouble with a cash system in my game. In this game I’m working on, if you pick up a “robuck,” it will give you +5 cash. Upon buying something for 50 cash, it subtracts the cash accordingly, however, if you then pick up another robuck, it will add another 5 cash along with the 50 you just lost.

Here’s a video showing the problem:

This seems like a simple problem to fix, but everything I try doesn’t seem to work. Does anyone know what to do?

(here’s the code in the robuck)

local clickable = true

game.Players.PlayerAdded:Connect(function(plr)
	script.Parent.PickUp.Triggered:Connect(function()
	
		if clickable == true then 
			clickable = false
plr.Character.Sound:Play()
			plr.leaderstats.cash.Value = plr.leaderstats.cash.Value + 5
			script.Parent:Remove()
		else
			print("already picked up")
		end
	end)
end)

Thanks,
Mustang

1 Like

Could be a client, server problem. You’re probably removing the currency on client and when you pick up money you add it on the server! Make both server sided!

2 Likes

Thanks for the reply, I’m pretty sure this is the issue. Do you know how I could get the player’s leaderstat value and subtract 50 from it?

1 Like

you could create a remote event in replicatedstorage and when you want to remove the leaderstat value you just check the players money and subtract 50 from the players value

remote.OnServerEvent:Connect(function(plr)

if plr.Money.Value >= 50 then

plr.Money.Value = plr.Money.Value - 50

end

end)
1 Like

You don’t need the playerAdded as one of the parameters of a proximity script is the player. You’re also checking ‘clickable’ even though the part is going to be removed. I edited your script and see if this works.

script.Parent.PickUp.Triggered:Connect(function(plr)
	plr.Character.Sound:Play()
	plr.leaderstats.cash.Value += 5
	script.Parent:Remove()
end)
1 Like

You should also send a remote event to the player if you want to play the sound locally.

2 Likes

Thank you so much! This works very well!