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

Give GamePass owners weapon if they own the GamePass?

Title.

I own the GamePass, so I receive the item while testing. But, when I run the game for real (outside of Studio) I don't receive the GamePass.

F.Y.I, I used the default script from the Dev Article here.

Code:

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
 
local gamePassID = 4973716  -- Change this to your game pass ID
 
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, gamePassID)
    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 " .. gamePassID)
        
        --The Below code gives the Gamepass owner the Gamepass

        local sword = game.ServerStorage["Death Hammer"]:Clone()

        sword.Parent = game.Players.LocalPlayer.Backpack
        sword:Clone().Parent = game.Players.LocalPlayer.StarterGear

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

Thanks, Mex

2 comments

0

LocalPlayer is nil on the server

0

Oh, I see. What would I call instead? Thanks, again

1 answer

--[[
    LocalPlayer is nil on the server. 
    The PlayerAdded event passes the Player as a parameter, so use the Player parameter instead. 
]]--

wait()
local ServerStorage = game:GetService("ServerStorage")
local hammer = ServerStorage["Death Hammer"]
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local gamePassID = 4973716  -- Change this to your game pass ID

local function onPlayerAdded(player) -- player parameter, use this 
    local hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)

    if hasPass then
        print(player, "owns the game pass with ID",  gamePassID)
        --The Below code gives the Gamepass owner the Gamepass

        local sword = hammer:Clone()

        sword.Parent = player.Backpack -- LocalPlayer is nil on the server
        sword:Clone().Parent = player.StarterGear 
    end
end

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

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