Hello, I have added a Buy Cash GUI in my zed tycoon game. Everything works except for after I buy the product, it does not give my cash in my cash value.
I have made a value, but you can change it a bit if you want! but I think this will certainly works!
local MarketplaceService = game:GetService("MarketplaceService")
local productID = YOUR_PRODUCT_ID
local cashAmount = 100 -- The amount of in-game cash to give to the player upon purchase
local function giveCash(player)
local cashValue = player:FindFirstChild("Cash")
if not cashValue then
cashValue = Instance.new("IntValue")
cashValue.Name = "Cash"
cashValue.Parent = player
end
cashValue.Value = cashValue.Value + cashAmount
print(player.Name .. " received " .. cashAmount .. " in-game cash!")
end
local function onPromptGamePassPurchaseFinished(player, purchasedPassId, purchaseSuccess)
if purchaseSuccess and purchasedPassId == productID then
-- Check if the purchased pass ID matches the desired product ID
giveCash(player)
end
end
game.Players.PlayerAdded:Connect(function(player)
player.promptGamePassPurchaseFinished:Connect(function(purchasedPassId, purchaseSuccess)
onPromptGamePassPurchaseFinished(player, purchasedPassId, purchaseSuccess)
end)
end)
-- Optional: Check if players already own the game pass when the server starts
game.Players.PlayerAdded:Connect(function(player)
local ownPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, productID)
if ownPass then
giveCash(player)
end
end)
I get a error 13:26:59.605 Players.TersaiIor.PlayerGui.TweenGuiTest.Frame1.TextButton.LocalScript:9: attempt to perform arithmetic (add) on string and number - Client - LocalScript:9
And a warning 13:27:04.072 Error occurred while processing a product purchase - Server - ReceiptHandler:135
local MarketplaceService = game:GetService(“MarketplaceService”)
local productID = 1563193650
local cashAmount = 1000000 – The amount of in-game cash to give to the player upon purchase
local function giveCash(player)
local cashValue = player:FindFirstChild(“Cash”)
if not cashValue then
cashValue = Instance.new(“IntValue”)
cashValue.Name = “Cash”
cashValue.Parent = player
end
local function onPromptGamePassPurchaseFinished(player, purchasedPassId, purchaseSuccess)
if purchaseSuccess and purchasedPassId == productID then
– Check if the purchased pass ID matches the desired product ID
giveCash(player)
end
end
So would a code like this work? (Im a beginner so I use AI chat to help)
MPS = game:GetService(“MarketplaceService”)
id = 1563193650
local player = game.Players.LocalPlayer
– create a remote function
local cashUpdater = game.ReplicatedStorage:WaitForChild(“CashUpdater”)
script.Parent.MouseButton1Click:connect(function()
MPS:PromptProductPurchase(player, id)
– fire the remote function after the purchase
cashUpdater:FireServer(100000)
end)
– on the server-side, create the server script that listens for the remote function and updates the player’s cash
local cashUpdater = game.ReplicatedStorage:WaitForChild(“CashUpdater”)
cashUpdater.OnServerEvent:Connect(function(player, amount)
– update the player’s cash
player.leaderstat.Cash.Value = player.leaderstat.Cash.Value + amount
end)
MPS = game:GetService("MarketplaceService")
id = 1563193650
local player = game.Players.LocalPlayer
-- create a remote function
local cashUpdater = game.ReplicatedStorage:WaitForChild("CashUpdater")
script.Parent.MouseButton1Click:connect(function()
MPS:PromptProductPurchase(player, id)
-- fire the remote function after the purchase
cashUpdater:FireServer(100000)
end)
-- on the server-side, create the server script that listens for the remote function and updates the player's cash
local cashUpdater = game.ReplicatedStorage:WaitForChild("CashUpdater")
cashUpdater.OnServerEvent:Connect(function(player, amount)
-- update the player's cash
player.leaderstat.Cash.Value = player.leaderstat.Cash.Value + amount
end)
local MPS = game:GetService("MarketplaceService")
MPS.ProcessReceipt = function(recieptInfo)
if recieptInfo.ProductId == YOUR_PRODUCT_ID then
local plr = game.Players:GetPlayerByUserId(recieptInfo.PlayerId)
plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value +AMMOUNT_HERE
return Enum.ProductPurchaseDecision.PurchaseGranted
end
end