So I'm working on a game where you earn 1 coin every 3 seconds. I added a shop that allows players to buy more coins where they can purchase in-game items with.

However, when a player buys for example the +100 cash product and their current amount of coins is 21, the amount after the purchase will be 100 cash instead of 121.

The constant earnings is a simple LocalScript inside the StarterCharacterScripts:

local player = game.Players:WaitForChild(script.Parent.Name)
local coins = player:WaitForChild("leaderstats").Coins

while true do
	wait(3)
	coins.Value = coins.Value + 1
end

The script that handles the purchases is a Script inside the ServerScriptService:

local MPS = game:GetService("MarketplaceService")

MPS.ProcessReceipt = function(receiptInfo)
	local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
	local coinsValue = player.leaderstats.Coins.Value
	print(coinsValue) --Just to test, this is where I get 0 instead of the actual value
	
	if receiptInfo.ProductId == 123456789 then
		player.leaderstats.Coins.Value = coinsValue + 100
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

And a third script is the placement module. Basically it lowers your money by the price of the item after you place it. This is in a ModuleScript inside the StarterGui.

local function place()
	--This is where the part will be placed
		
		local Player = game.Players:FindFirstChild(script.Parent.Parent.Parent.Parent.Parent.Name)
		Player.leaderstats.Coins.Value = Player.leaderstats.Coins.Value - script.Parent.CurrentPrice.Value
		script.Parent.CurrentPrice.Value = 0
		print("complete")		
	end
end

Is there any way to make these scripts work together and make the purchase handler script recognize the amount of cash the player already has? Thank you already

4 comments

0

Instead of assigning a variable the coins just use the player.leaderstats.coins.valur

0

Try that, or provide any output errors.

0

I've done that, and there are no output errors

0

I’m not sure then, try printing player.leaderstats.coin.value before putting it into a variable.