Coins value keeps reverting

Hello!

I have a script that makes an effect on the screen to add coins, rather than just changing the text to the new value.

The issue is that right after the second purchase, the value reverts to the old one.

Purchase Handler Script
m = game:GetService("MarketplaceService")

m.PromptProductPurchaseFinished:Connect(function(userid, productid, purchased)
	local player

	if purchased == true then
		for i,v in pairs(game.Players:GetChildren()) do
			if v.UserId == userid then
				player = v
			end
		
			local value = game.ServerStorage.ProductInfo[productid].Value

			game.ReplicatedStorage.CoinAdded:Fire(game.Players[player.Name].inventory.Coins.Value, value, player) -- Player's coin value, value to add, and the player
		end
	end
end)
Script to update player's coins, gets the old one, should set the new one.
game.ReplicatedStorage.CoinAdded.Event:Connect(function(coinobj, coinstoadd, player)

	local oldcoin = coinobj
	local newcoin = coinobj + coinstoadd 

	coinobj = newcoin
	
	print(oldcoin.."oldcoin "..newcoin.."newcoin")
	
	game.ReplicatedStorage.CoinEffect:FireClient(player, oldcoin, newcoin)
	
end)
Script to update the text on the player's screen.
game.ReplicatedStorage.CoinEffect.OnClientEvent:Connect(function(oldcoin, newcoin)
	
	for i = oldcoin, newcoin, 10 do
		script.Parent.Text = "Coins: "..i
		wait()
	end

end)

It doesn’t look like you’re updating the player’s inventory Coins value, so you’re referencing and adding to an outdated value.

I think I am, these lines do that I believe.
.CoinAdded:Fire(game.Players[player.Name].inventory.Coins.Value (script 1 - arg 1)
.Event:Connect(function(coinobj, coinstoadd, player) (script 2 - arg 1)
coinobj = newcoin (script 2 - what sets the coins value)

When you reference the .Value of an IntValue, you will get an integer that is no longer tied to the player’s Coins value.

You will need to do something like this:

player.inventory.Coins.Value = player.inventory.Coins.Value + coinstoadd
2 Likes

This script is going to error on a server with more than one player.

local value = game.ServerStorage.ProductInfo[productid].Value
game.ReplicatedStorage.CoinAdded:Fire(game.Players[player.Name].inventory.Coins.Value, value, player)

those lines should be placed into the if statement, or they will fire for every player in the game

1 Like

Right, I probably could have put the value object there. Thanks! It worked. Seems like a stupid error in retrospect, not sure why I couldn’t find that.

Didn’t notice. Thanks for pointing it out!