Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do i add cash to a player from a gamepass?

Im trying to make a game pass that gives a bunch of cash when bought however I don't know how to properly access the leader stats to add more cash. ** This is my Code for the server script part of the game pass**

local RS = game:GetService("ReplicatedStorage")
local sword = RS:WaitForChild("Sword")
local currencyName = "Cash"
local gamePass = game:GetService("MarketplaceService")
gamePassId = 15489874

gamePass.PromptGamePassPurchaseFinished:Connect(function(player, passId, purchaseSuccess)
	if purchaseSuccess and passId == gamePassId then 
		local player = game.Players:GetPlayers()
local stats = player:FindFirstChild("leaderstats")
stats[currencyName].Value = stats[currencyName].Cash.Value + 34567
		print(" purchases was successful")
	end
	
end)

**This is my code for the local script **

local gamePass = game:GetService("MarketplaceService")
gamePassId = 15489874
local function Pass ()
local player = game.Players.LocalPlayer
	local check = false 
	local success, error = pcall(function()
		check = gamePass:UserOwnsGamePassAsync(player.UserId, gamePassId)
	end)
	if check then
		gamePass:PromptGamePassPurchase(player, gamePassId)
		print("Pass owned")
	else 
		gamePass:PromptGamePassPurchase(player, gamePassId)
		print("Getting pass")
	end

end
wait(5)
Pass()

3 comments

0

have you tried using remote functions?

0

'GetPlayers' will return a collection of the Player Objects under the 'Players' container. Since it is an array, you're going to get a conflict on line 10. You need the individual Player Instance, which is fortunately provided by 'PromptGamePassPurchaseFinished' when executing your callback. You've already allocated the parameter, simply use it.

0

Another thing you should keep in mind is that GamePasses are one-time purchases. When buying in-game currency, this isn't ideal. You're going to want to look into Developer Products, as they will allow perpetual purchases.

1 answer

This is the code that I would use hope it helps just make another script that is the leader board and change the value of cash other than that you can just copy and paste this. if this helps please mark it as answered.


local Cash = game.Players.LocalPlayer.leaderstats.Cash -- Don't add .Value at the end of this
local Amount = 100

local passId = 15489874
local marketplaceService = game:GetService("MarketplaceService")

marketplaceService.PromptPurchaseFinished:connect(function(player,assetId,isPurchased)
    if isPurchased then
        if assetId == passId then
            Cash.Value = Cash.Value + Amount
        end
    end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.