Help with saving a game pass item

Hi, so I made a script so that when you can purchase a gravity coil game pass. But when I leave and rejoin, it doesn’t save. This is the script that is in ServerScriptService

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

local GravityCoilID = 17792555

game.Players.PlayerAdded:Connect(function(player)
	
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, GravityCoilID)
	end)

if hasPass then
	print("player already owns game pass")
	
		local GravityCoil = game.ServerStorage.GravityCoil:Clone()
	GravityCoil.Parent = player.Backpack
	end
end)

local function onPromtGamePassPurchaseFinished(player, purchasedPassID, purchaseSuccess)
	
	if purchaseSuccess == true and purchasedPassID == GravityCoilID then
		print(player.Name .. "purchased game pass")
		
		local GravityCoil = game.ServerStorage.GravityCoil:Clone()
		GravityCoil.Parent = player.Backpack
	end
end

MarketplaceService.PromptGamePassPurchaseFinished:Connect(onPromtGamePassPurchaseFinished)

If anyone can help me figure out how to save the item to their account when they leave, it will be greatly appreciated.

Thank You.

Did you forget to include a local variable for sanity checks inside the PlayerAdded event?

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

local GravityCoilID = 17792555

game.Players.PlayerAdded:Connect(function(player)
	local hasPass = false
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, GravityCoilID)
	end)

if hasPass then
	print("player already owns game pass")
	
		local GravityCoil = game.ServerStorage.GravityCoil:Clone()
		GravityCoil.Parent = player.Backpack

		local GravityCoil = game.ServerStorage.GravityCoil:Clone()
		GravityCoil.Parent = player.StarterGear
	end
end)

local function onPromtGamePassPurchaseFinished(player, purchasedPassID, purchaseSuccess)
	
	if purchaseSuccess == true and purchasedPassID == GravityCoilID then
		print(player.Name .. "purchased game pass")
		
		local GravityCoil = game.ServerStorage.GravityCoil:Clone()
		GravityCoil.Parent = player.Backpack

		local GravityCoil = game.ServerStorage.GravityCoil:Clone()
		GravityCoil.Parent = player.StarterGear
	end
end

MarketplaceService.PromptGamePassPurchaseFinished:Connect(onPromtGamePassPurchaseFinished)

Also you could just clone the Gear into their StarterGear as well if you want them to keep the tool entirely once they purchase the Gamepass

1 Like