Help with GamePass system

  1. What do you want to achieve? I’m attempting to make a gamepass system, where when you buy a gamepass, you get an item, and when you rejoin, you get the item.

  2. What is the issue? The rejoin part is working, but the part in the code where it checks if the user purchased isn’t working and is only registering IsPurchesed as false, even if a user clicks buy.

  3. What solutions have you tried so far? Searching development hub, going through the API documentation.
    Code:

local id = 12345
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local passname = "Hitbox" --Set this to whatever the gamepass name 
local ui = game.StarterGui


Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function()
        if MarketplaceService:UserOwnsGamePassAsync(player.UserId, id) then 
            print("User owns pass")
            for i, v in pairs(script.ToolFolder:GetChildren()) do
                local clone = v:Clone()
                clone.Parent = player.Backpack
                
            end
        else
            
        end
    end)
end)

game.Workspace.WorkspaceParts.TouchPart.Touched:Connect(function(touched)

    local player = game:GetService("Players"):GetPlayerFromCharacter(touched.Parent)
    if player then
        print("Part touched!")
        game:GetService("MarketplaceService"):PromptGamePassPurchase(player, id)
        print("Prompt fired!")
    end
end)

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, isPurchased)
    if isPurchased == true then
        print(player.Name.."Owns product!")
        for i, v in pairs(script.ToolFolder:GetChildren()) do
            local clone = v:Clone()
            clone.Parent = player.Backpack
            end
        ui.gamepassGui.TextLabel.Text = player.Name.. "bought the" ..passname.. "gamepass!"
        ui.gamepassGui.TextLabel.Visible = true
        wait(5)
        ui.gamepassGui.TextLabel.Visible = false

    else
        print(player.Name .. " didn't buy an item")
    end
end)

PromptGamePassPurchaseFinished returns the player instance, the id of what was bought and if the purchase was successful, the 2nd return is the id, so isPurchased must be the third argument

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, id, isPurchased)

Edit: you’re also getting the ui from StarterGui, get it from the player’s playergui, you have the player instance already

1 Like

Ah thanks, this was very helpful!

1 Like