GamePass script issue

Hey guys I’m new to scripting and have been having some issues with my GamePass script. If anyone could lend a hand I would highly appreciate it!

  1. What do you want to achieve? Upon purchasing the gamepass, the tool goes into the player’s inventory and remains there even after death. Also the player should still have the tool after rejoining the game.

  2. What is the issue? The tool disappears from the inventory upon death, and doesn’t appear after rejoining the game either.

  3. What solutions have you tried so far? Already tried several things, but nothing seems to work.

Here is my code:

local MarketPlaceService = game:GetService("MarketplaceService")

local Players = game:GetService("Players")

local gpIDs = {
	00000001, -- gravity
	00000002, -- balloon
	00000003, -- grapple
	00000004, -- rainbow
	00000005, -- boombox
	00000006, -- fusion
	00000007, -- speed
}

local rewards = {
	game.ServerStorage.GravityCoil,
	game.ServerStorage.GreenBalloon,
	game.ServerStorage.GrappleHook,
	game.ServerStorage.RainbowMagicCarpet,
	game.ServerStorage.BoomBox,
	game.ServerStorage.FusionCoil,
	game.ServerStorage.SpeedCoil
}

game.Players.PlayerAdded:Connect(function(player)
	for index, pass in ipairs(gpIDs) do
		local success, message = pcall(function()
			hasPass = MarketPlaceService:UserOwnsGamePassAsync(player.UserID, pass)
		end)

		if hasPass then
			print("player has the game pass")
			local tool = rewards[index]:Clone()
			tool.Parent = player.Backpack
			tool.Parent = player.StarterGear
		else
			print("gear obtained")
		end
	end
end)

local function onPromptGamePassPurchaseFinished(player, purchasedPassID, purchaseSuccess)
	if purchaseSuccess then
		for index, pass in ipairs(gpIDs) do
			if purchasedPassID == pass then
				print(player.Name .. " purchased item.")

				local tool = rewards[index]:Clone()
				tool.Parent = player.Backpack
			end
		end
	end
end

MarketPlaceService.PromptGamePassPurchaseFinished:Connect(onPromptGamePassPurchaseFinished)

Also any suggestions on how to improve the code are appreciated! Thanks!

When the character dies, you should clone the item again and parent it to the player’s backpack.

Humanoid.Died:Connect(function()
Player.CharacterAdded:Connect(function()
-- If needed, check if the player owns gamepass here
Tool:Clone().Parent = Player.Backpack
end)
end)
1 Like

Thanks for the reply! In the meantime I managed to fix the issue of the tool not remaining after death by simply adding it to the StarterGear.

local function onPromptGamePassPurchaseFinished(player, purchasedPassID, purchaseSuccess)
	if purchaseSuccess then
		for index, pass in ipairs(gpIDs) do
			if purchasedPassID == pass then
				print(player.Name .. " purchased item.")

				local tool = rewards[index]:Clone()
				tool.Parent = player.Backpack
                -- Clone the tool into the StarterGear
				tool:Clone().Parent = player.StarterGear
			end
		end
	end
end

Still the issue remains that the tools do not appear in the players inventory after they have rejoined the game.

1 Like

player.UserID is nil. It’s supposed to be player.UserId, and because you used pcall, there’s no error, and you used it incorrectly, kinda. Here, try this one instead.

local success, hasPass = pcall(function()
	return MarketPlaceService:UserOwnsGamePassAsync(player.UserId, pass)
end)
if success and hasPass then --// make sure it worked, the second variable returns the boolean from userownsgamepassasync
print'player has pass'
else
print'player does not have pass'
end;

Please read this to see how pcall works.

Or you can use pcall like this.

local success, hasPass = pcall(MarketPlaceService.UserOwnsGamePassAsync, MarketPlaceService, player.UserId, Pass)
warn(success,hasPass)
1 Like

This worked perfectly! Thank you for the help and the thorough explanation!