How to detect if a player owns a gamepass

I made this script below which is intended to detect if a player is on a certain team and owns a gamepass; if they are / have both, then it should give them a tool from a folder in ServerStorage, however this is not working; there are no errors either…

anyone have any ideas why this would be?

local BCSO_TEAM = game.Teams.BCSO
local OPD_TEAM = game.Teams.OPD
local toolsFolder = game.ServerStorage["SWAT Tools"]

game.Players.PlayerAdded:Connect(function(player)
	local ownsGamePass = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, 28301022)
	if player.Team == BCSO_TEAM and ownsGamePass then
		for _,v in ipairs(toolsFolder["SRT Shield"]()) do
			v:Clone().Parent = player.Backpack
		end
	end
end)
1 Like
if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(game.Players.LocalPlayer.UserId, ID_HERE) == true then
	-- do stuff
end

It might not be the gamepass part of the function but the checking to see what team they are on seeing as you are firing this function 2 lines after they join

So would this be the replacement with that being kept in mind?

local BCSO_TEAM = game.Teams.BCSO
local OPD_TEAM = game.Teams.OPD
local toolsFolder = game.ServerStorage["SWAT Tools"]

game.Players.PlayerAdded:Connect(function(player)
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(game.Players.LocalPlayer.UserId, 28301022) == true then
		for _,v in ipairs(toolsFolder["SRT Shield"]()) do
			v:Clone().Parent = player.Backpack
		end
	end
end)

This script works:

--//Services
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local MarketplaceService = game:GetService("MarketplaceService")
local Teams = game:GetService("Teams")

--//Variables
local BCSO_TEAM = Teams.BCSO
local OPD_TEAM = Teams.OPD
local toolsFolder = ServerStorage:WaitForChild("SWAT Tools")

--//Functions
Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, 28301022) and Player.Team == BCSO_TEAM then
			for i, Child in ipairs(toolsFolder:WaitForChild("SRT Shield"):GetChildren()) do
				Child:Clone().Parent = Player.Backpack
			end
		end
	end)
end)

I’ll check this out! Im assuming this goes in a regular Script under ServerScriptService correct?

This doesn’t appear to be working. No errors either.

UPDATE: I fixed it, I had to alter the script a tiny bit, like shown below. Thanks for the help!

--//Services
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local MarketplaceService = game:GetService("MarketplaceService")
local Teams = game:GetService("Teams")

--//Variables
local BCSO_TEAM = Teams.BCSO
local OPD_TEAM = Teams.OPD
local toolsFolder = ServerStorage:WaitForChild("SWAT Tools")

--//Functions
Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, 28301022) and Player.Team == BCSO_TEAM then
		local tools = toolsFolder["SRT Shield"]
			tools:Clone().Parent = Player.Backpack
		end
	end)
end)
--//Services
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local MarketplaceService = game:GetService("MarketplaceService")
local Teams = game:GetService("Teams")

--//Variables
local BCSO_TEAM = Teams.BCSO
local OPD_TEAM = Teams.OPD
local toolsFolder = ServerStorage:WaitForChild("SWAT Tools")

--//Functions
Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, 28301022) and Player.Team == BCSO_TEAM then
		local tools = toolsFolder["SRT Shield"]
			tools:Clone().Parent = Player.Backpack
		end
	end)
end)
1 Like

No problem, have a good day.

30

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

local BCSO_TEAM = Teams.BCSO
local OPD_TEAM = Teams.OPD
local toolsFolder = ServerStorage["SWAT Tools"]
local srtShield = toolsFolder["SRT Shield"]

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		local Backpack = Player:WaitForChild("Backpack")
		if Player.Team == BCSO_TEAM then
			local Success, Result = pcall(function()
				return MarketplaceService:UserOwnsGamePassAsync(Player.UserId, 28301022)
			end)
			
			if Success then
				if Result then
					local shieldClone = srtShield:Clone()
					shieldClone.Parent = Backpack
				end
			else
				warn(Result)
			end
		end
	end)
end)

None of the “:WaitForChild()” calls aren’t necessary as everything is being instanced by the server anyway. Other than that, API requests should be wrapped in pcall() calls because if Roblox’s servers/API servers go offline/become unresponsive then the request would error causing the entire script to terminate abruptly/prematurely.

1 Like