After purchasing a product with in-game currency, collecting more money just gives you the money you spent back

I have a currency in my game called shells. You can use them to buy things, like a tan. After buying the product, collecting a shell that normally gives you 1 shell gives you all the money you just spent on the tan + 1 shell. It should only be giving you 1 shell. I can’t find the cause of this. I will link a screenshot of the code.
Screen Shot 2021-01-31 at 3.56.12 PM|690x431
The portion of the code that is highlighted is the code that is taking away the money.
The rest of the code is what is changing your skin tone color, but I don’t think that part matters.

I’ve had this problem before. It’s because you’re changing the currency in a localscript instead of a normal script. Everything that changes the currency should be in a normal script or the server wouldn’t see the change.

Edit: Use a remote event
put a remote event in ReplicatedStorage named Purchase and replace the part in the localscript that subtracts money with this below

--  In localscript
--change AMOUNT to the number to subtract
game.ReplicatedStorage.Purchase:FireServer(AMOUNT)

Make a new script in serverscriptservice

--in a script in serverscriptservice

game.ReplicatedStorage.Purchase.OnServerEvent:Connect(function(player,amount)
	player.Leaderstats.Shells.Value=player.Leaderstats.Shells.Value-amount
end)

Okay, thank you. In the script in serverscriptservice, should I change the part that says player.Leaderstats.Shells.Value- amount to player.Leaderstats.Shells.Value-45 or leave it as amount

Leave it as amount but change “AMOUNT” in the local script to 45. You can use the same remote event in any localscript for all purchases in the game but change “AMOUNT” to the cost.

Okay so I changed the scripts, but now when I get enough shells and purchase the product, it doesn’t take away any shells. Here is what the local script looks like.

oh that might be because instead of an uppercase L in leaderstats

player.Leaderstats.Shells.Value= player.Leaderstats.Shells.Value-45

it should be a lowercase a L

player.leaderstats.Shells.Value=  player.leaderstats.Shells.Value-45

Okay, I changed it and it worked. Thank you!

1 Like