Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago

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()
0
have you tried using remote functions? 0hsa 193 — 3y
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. Ziffixture 6913 — 3y
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. Ziffixture 6913 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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)
Ad

Answer this question