So i’ve been trying to give players abilities when they purchase a gamepass. And I searched the developer hub for help and copied the code but… it gives the player who owns the gamepass the ability but it also gives the non-gamepass owners the ability.
local MarketplaceService = game:GetService(“MarketplaceService”)
local Players = game:GetService(“Players”)
local gamePassID = ********
local function onPlayerAdded(player)
local hasPass = false
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)
local serverstorage = game:GetService("ServerStorage")
local sword = serverstorage:FindFirstChild("ClassicSword")
sword:Clone()
sword.Parent = game.StarterPack
end
end
– Connect “PlayerAdded” events to the “onPlayerAdded()” function
Players.PlayerAdded:Connect(onPlayerAdded)
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local gamePassID = ********
local function onPlayerAdded(player)
local hasPass = false
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)
local serverstorage = game:GetService("ServerStorage")
local sword = serverstorage:FindFirstChild("ClassicSword")
sword:Clone()
sword.Parent = game.StarterPack
end
end
--Connect "PlayerAdded" events to the "onPlayerAdded()" function
Players.PlayerAdded:Connect(onPlayerAdded)
This is probably why, the sword is being cloned to the StarterPack itself so it’s gonna give it to all players when they respawn back
Try this script:
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local gamePassID = ********
local function onPlayerAdded(player)
local hasPass = false
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)
local serverstorage = game:GetService("ServerStorage")
local sword = serverstorage:FindFirstChild("ClassicSword")
sword:Clone()
sword.Parent = player.Backpack
local sword2 = serverstorage:FindFirstChild("ClassicSword")
sword2:Clone()
sword2.Parent = player.StarterGear
end
end
--Connect "PlayerAdded" events to the "onPlayerAdded()" function
Players.PlayerAdded:Connect(onPlayerAdded)
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local gamePassID = your id
local function onPlayerAdded(player)
local hasPass = false
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
else
if hasPass == true then
local serverstorage = game:GetService("ServerStorage")
local sword = serverstorage:FindFirstChild("ClassicSword")
local clone = sword:Clone()
sword.Parent = player.StarterGear
local clone2 = sword:Clone()
clone2.Parent = player.Backpack
end
end
end
-- Connect “PlayerAdded” events to the “onPlayerAdded()” function
Players.PlayerAdded:Connect(onPlayerAdded)
Bit confused, the hasPass variable will be set to false every time a Player joins since it’s encased inside the onPlayerAdded function so it’ll be different for individual players?
Good point! I didn’t notice it because it wasn’t formatted properly, and I see a lot of people making that mistake and I’ve made that mistake a lot when I started learning
Ok strange, probably made 2 variables when I should’ve just made 1
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local gamePassID = ********
local function onPlayerAdded(player)
local hasPass = false
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)
local serverstorage = game:GetService("ServerStorage")
local sword = serverstorage:WaitForChild("ClassicSword")
sword:Clone()
sword.Parent = player.Backpack
sword:Clone()
sword.Parent = player.StarterGear
end
end
--Connect "PlayerAdded" events to the "onPlayerAdded()" function
Players.PlayerAdded:Connect(onPlayerAdded)