Gamepass to give player an item

Gamepass to give player an item

I am making a game, and wanted to implement gamepasess, but I can't seem to figure out how to make it so if the gamepass is purchased, then the player is given Tools when they join, I've had a look at MarketplaceService in developer.roblox.com, but it makes no sense to me, could anyone put it into simpler words?

thanks

Here is what you are looking for: UserOwnsGamepassAsync()

Using this is incredibly simple, here’s an example.

local MarketplaceService = game:GetService("MarketplaceService")
game.Players.PlayerAdded:Connect(function(player)
    local OwnsGamepass = MarketplaceService:UserOwnsGamepassAsync(player.UserId, GAMEPASS_ID)
    if OwnsGamepass then -- returned true
        -- do stuff here
    end -- no need for an else
end)

Edit: fixed formatting sorry lol

5 Likes

@b_oolean I have tried to make it so certain players get items too, but it just says the players backpack doesn’t exist

Local player = game.Players.LocalPlayers.Name
if player == 'player1' then
    game.replicatedStorage.Tool.Parent = game.Players.LocalPlayer.Backpack

this just leaves an error saying the players Backpack doesn’t exist

Do wait for child and also you shouldnt be giving the tools on the client. If you give them on the client they cant be used server wide and will only affect the given player. Consider using a remote event once the person has been confirmed to own it.

Edit: Also player is an object should be player.Name ==

1 Like

Just to add to @b_oolean’s post, it’s a good practice to wrap calls like these in pcalls since MarketplaceService:UserOwnsGamepassAsync() makes a web call. Web calls have a chance to fail if the Roblox servers are down, the gamepass ID is invalid etc.

local MarketplaceService = game:GetService("MarketplaceService")
game.Players.PlayerAdded:Connect(function(player)
    local Success, OwnsGamepass = pcall(function()
        return MarketplaceService:UserOwnsGamepassAsync(player.UserId, GAMEPASS_ID)
    end)

    local Retries = 0
    while not Success do
        Retries = Retries + 1
        if Retries == 5 then break end
        Success, OwnsGamepass = pcall(function()
            return MarketplaceService:UserOwnsGamepassAsync(player.UserId, GAMEPASS_ID)
        end)
        wait(1)
    end

    if not Success then
        error(OwnsGamepass)
        return
    end

    if OwnsGamepass then -- returned true
        -- do stuff here
    end -- no need for an else
end)

You made some mistakes with variable names

Which ones? Nothing looks wrong to me.