Giving players an ability when they purchase a gamepass

Hello,

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.

Can someone help me with this?

1 Like

Could we see the script that handles the ability gamepass? It’d help

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)

Just gonna format your code lol

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)
2 Likes

Okay your error here is that you are setting the variable haspass for everyone on the server.

Example if player1 joins and has pass it will set the variable to true, then if player2 joins, haspass will already be set to true.

You would have to use a table instead

local haspass = {}

then if you want to add to the table when someone has a pass you would do

haspass[player] = true

then if you want to check if they have pass you would do

if haspass[player] == true then 

Change your code to


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)

Make sure to put your gamepass id!

1 Like

Sorry, didn’t see your reply while I was typing.

1 Like

I tried you’re script but now noone has the sword.

1 Like

nvm I forgot to chage the gamepass id oof.

1 Like

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?

I might be corrected wrong though

2 Likes

It gives this error;

serverscriptservice.Script, line 30 attempt to index nil with ‘clone’

1 Like

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

2 Likes

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)
2 Likes

Now it works!

Thanks for everyones help.

1 Like