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

If Player Has Bough GamePass/Developer Product To Get A Tool?

Asked by 5 years ago

If Player has game Pass, When Joins Server To Have A Specific Tool, I Tried but it doesn't work!

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

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

function onPlayerSpawned(player) 

    local hasPass = false

    -- Check if the player already owns the game pass
    local success, message = pcall(function()
        hasPass = MarketplaceService:UserOwnsGamePassAsync(player.userId, gamePassID)
  print("Has Game Pass for Gun")
    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
  game.ServerStorage.GravityCoil:clone().Parent = player.Backpack
 end
end 

game.Players.PlayerAdded:connect(function(player)
  player.CharacterAdded:connect(function()
    onPlayerSpawned(player)
  end)
end)

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

3 answers

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

Try this..

Be sure to capitalize Connect and Clone, and we also changed PlayerSpawned to PlayerAdded, because PlayerSpawned does not work.

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

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

function onPlayerSpawned(player) 

    local hasPass = false

    -- Check if the player already owns the game pass
    local success, message = pcall(function()
        hasPass = MarketplaceService:UserOwnsGamePassAsync(player.userId, gamePassID)
  print("Has Game Pass for Gun")
    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
  game.ServerStorage.GravityCoil:Clone().Parent = player.StarterGear
 end
end 

-- Connect 'PlayerAdded' events to the 'onPlayerAdded()' function
Players.PlayerAdded:Connect(onPlayerSpawned)
Ad
Log in to vote
0
Answered by 5 years ago

I see your problem, you never made it clear that the players hasPass is true or not. Use this script instead:

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

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

function onPlayerSpawned(player) 

    local hasPass = false

    -- Check if the player already owns the game pass
    local success, message = pcall(function()
        hasPass = MarketplaceService:UserOwnsGamePassAsync(player.userId, gamePassID)
 hasPass = true
    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
  game.ServerStorage.GravityCoil:clone().Parent = player.Backpack
 end
end 

game.Players.PlayerAdded:connect(function(player)
  player.CharacterAdded:connect(function()
    onPlayerSpawned(player)
  end)
end)

Log in to vote
0
Answered by 5 years ago

I see your problem, you never made it clear that the players hasPass is true or not. Use this script instead:

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

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

function onPlayerSpawned(player) 

    local hasPass = false

    -- Check if the player already owns the game pass
    local success, message = pcall(function()
        hasPass = MarketplaceService:UserOwnsGamePassAsync(player.userId, gamePassID)
 hasPass = true
    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
  game.ServerStorage.GravityCoil:clone().Parent = player.Backpack
 end
end 

game.Players.PlayerAdded:connect(function(player)
  player.CharacterAdded:connect(function()
    onPlayerSpawned(player)
  end)
end)

Answer this question