Gamepasses not working

I am making a gun gamepass script but it has this weird property that if you test purchase the game pass in studio it works but if you buy it in the actual game it doesn’t work. I’m not sure why this is and I can’t find anything wrong with my script

When Someone Buys The Gamepass Script
local MarketplaceService = game:GetService("MarketplaceService")

local Replicated = game:GetService("ReplicatedStorage")
local WeaponsFolder = Replicated:WaitForChild("Weapons")

local function GiveAllWeapons(player)
	for _, v in pairs(WeaponsFolder:GetChildren()) do
		if v.Name ~= "OwnerRifle" then
			local WeaponClone = v:Clone()
			WeaponClone.Parent = player.Backpack
		end
	end
end

local Weapons = {
	AR = 15199188,
	Shotgun = 15199196,
	SMG = 15199284,
	Pistol = 15199361,
	GrenadeLauncher = 15199367,
	RocketLauncher = 15199383
}  -- Change this to your game pass ID

-- Function to handle a completed prompt and purchase
local function onPromptGamePassPurchaseFinished(player, purchasedPassID, purchaseSuccess)

	if purchaseSuccess == true then
		for v, i in pairs(Weapons) do
			if purchasedPassID == 15199160 then
				GiveAllWeapons(player)
				return
			end
			if purchasedPassID == i then
				local WeaponClone = WeaponsFolder:WaitForChild(v)
				WeaponClone.Parent = player.Backpack
			end
		end

	end
end

MarketplaceService.PromptGamePassPurchaseFinished:Connect(onPromptGamePassPurchaseFinished)
When someone with the gamepass joins the game script
local MarketplaceService = game:GetService("MarketplaceService")
local Replicated = game:GetService("ReplicatedStorage")
local WeaponsFolder = Replicated:WaitForChild("Weapons")


local Weapons = {
	AR = 15199188,
	Shotgun = 15199196,
	SMG = 15199284,
	Pistol = 15199361,
	GrenadeLauncher = 15199367,
	RocketLauncher = 15199383
}

local function GiveAllWeapons(player)
	for _, v in pairs(WeaponsFolder:GetChildren()) do
		if v.Name ~= "OwnerRifle" then
			local WeaponClone = v:Clone()
			WeaponClone.Parent = player.Backpack
		end
	end
end

local function TestForGamepass(player, id)
	local hasPass = false

	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.userId, id)
	end)
	-- The script below is for checking whether or not the player has the gamepass.
	if not success then
		warn("Error while checking if player has pass: " .. tostring(message))
		return
	end

	if hasPass == true then
		for i, v in pairs(Weapons) do
			if v == 15199160 then
				GiveAllWeapons(player)
				return
			end
			if v == id then
				local WeaponClone = WeaponsFolder:WaitForChild(i)
				WeaponClone.Parent = player.Backpack
			end
		end
	end
		
end


game.Players.PlayerAdded:Connect(function(Player)
	TestForGamepass(Player, 15199188) -- Rifle
	TestForGamepass(Player, 15199196) -- Shotgun
	TestForGamepass(Player, 15199284) -- SMG
	TestForGamepass(Player, 15199361) -- Pistol
	TestForGamepass(Player, 15199367) -- GrenadeLauncher
	TestForGamepass(Player, 15199383) -- RocketLauncher

end)

These Scripts don’t raise any errors at all.

Thank you so much if you could spot any flaws at all :slight_smile:

1 Like

alright, I found 2 errors. 1. in the bought gamepass script, you give the weapons to the player multiple times if they bought the all weapons gamepass because it’s in a for loop

local function onPromptGamePassPurchaseFinished(player, purchasedPassID, purchaseSuccess)
	if purchasedPassID == 15199160 then -- moved this out of the loop
		GiveAllWeapons(player)
		return
	end

	if purchaseSuccess == true then
		for v, i in pairs(Weapons) do
			if purchasedPassID == i then
				local WeaponClone = WeaponsFolder:WaitForChild(v)
				WeaponClone.Parent = player.Backpack
			end
		end
	end
end
  1. again, the same error as last time but in the already has script.
local function TestForGamepass(player, id)
	local hasPass = false

	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.userId, id)
	end)
	-- The script below is for checking whether or not the player has the gamepass.
	if not success then
		warn("Error while checking if player has pass: " .. tostring(message))
		return
	end

	if hasPass == true then
		if v == 15199160 then -- moved this out of the loop
			GiveAllWeapons(player)
			return
		end
		for i, v in pairs(Weapons) do
			if v == id then
				local WeaponClone = WeaponsFolder:WaitForChild(i)
				WeaponClone.Parent = player.Backpack
			end
		end
	end
end
3 Likes

Also, a 3rd error, (sorry about the late reply, i just ate.) is the fact “userId” is not capitalized properly, roblox is very sensitive with this type of stuff (already has script).

local function TestForGamepass(player, id)
	local hasPass = false

	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, id) --capitalized it
	end)
	-- The script below is for checking whether or not the player has the gamepass.
	if not success then
		warn("Error while checking if player has pass: " .. tostring(message))
		return
	end

	if hasPass == true then
		if v == 15199160 then -- moved this out of the loop
			GiveAllWeapons(player)
			return
		end
		for i, v in pairs(Weapons) do
			if v == id then
				local WeaponClone = WeaponsFolder:WaitForChild(i)
				WeaponClone.Parent = player.Backpack
			end
		end
	end
end
2 Likes