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

I can not figure out how to give players a tool if they have the gamepass?

Asked by
sunszu 0
5 years ago

I am trying to make a gamepass script to give players a tool when they join the game

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local Gamepasses = {
    ["5688925"] = game.ServerStorage["Entry Permit"],
    ["3688870"] = game.ServerStorage["Tommy Gun"],
    ["4653506"] = game.ServerStorage["M1 Garand"],
    ["3423531"] = game.ServerStorage["Knife"],
}


function CheckPasses(Player)
    for _PassID,_PassTool in pairs (Gamepasses) do
        if MarketplaceService:UserOwnsGamePassAsync( Player.UserId, _PassID ) then
            _PassTool:Clone().Parent = Player.Backpack
        end
    end
end

local function onPlayerAdded(Player)

    local hasPass = false

    -- Check if the player already owns the game pass
    local success, message = pcall(function()
        hasPass = MarketplaceService:UserOwnsGamePassAsync(Player.UserId, Gamepasses)
    end)

    -- If there's an error, issue a warning and exit the function
    if not success then
        warn("Error while checking if player has pass: " .. tostring(message))
        return
    end

    if hasPass == true then
        print(Player.Name .. " owns the game pass with ID " .. Gamepasses)
    end
    end

-- Connect 'PlayerAdded' events to the 'onPlayerAdded()' function
Players.PlayerAdded:Connect(onPlayerAdded)

Answer this question