How to make game pass tool code smoother and more reliable?

Hey guys, how would I make this code run smoother and more reliably? It doesn’t always give the tool as soon as they buy so they need to rejoin to get occasionally.

local MarketplaceService = game:GetService("MarketplaceService")
local SpeedCoil = 17930916
local GravityCoil = 17930931 
local EpicSword = 18079239 --fill

game.Players.PlayerAdded:Connect(function(player)
		print("Player added")
		if MarketplaceService:UserOwnsGamePassAsync(player.UserId , GravityCoil) then
			print("Gravity coil given")
			game.ServerStorage.JumpCoil:Clone().Parent = player.Backpack
		game.ServerStorage.JumpCoil:Clone().Parent = player.StarterGear
		end

		if MarketplaceService:UserOwnsGamePassAsync(player.UserId , SpeedCoil) then
			print("Speed coil given")
			game.ServerStorage.SpeedCoil:Clone().Parent = player.Backpack
			game.ServerStorage.SpeedCoil:Clone().Parent = player.StarterGear
	end
	
	if MarketplaceService:UserOwnsGamePassAsync(player.UserId , EpicSword) then
		print("Epic sword given")
		game.ServerStorage.EpicSword:Clone().Parent = player.Backpack
		game.ServerStorage.EpicSword:Clone().Parent = player.StarterGear
	end
end)

-- Function to handle a completed prompt and purchase
local function onPromptGamePassPurchaseFinished(player, purchasedPassID, purchaseSuccess)
	if purchaseSuccess == true then
		if purchasedPassID == SpeedCoil then
			game.ServerStorage.SpeedCoil:Clone().Parent = player.Backpack
			game.ServerStorage.SpeedCoil:Clone().Parent = player.StarterGear

		elseif purchasedPassID == GravityCoil then
			game.ServerStorage.GravityCoil:Clone().Parent = player.Backpack
			game.ServerStorage.GravityCoil:Clone().Parent = player.StarterGear
			
		elseif purchasedPassID == EpicSword then
			game.ServerStorage.EpicSword:Clone().Parent = player.Backpack
			game.ServerStorage.EpicSword:Clone().Parent = player.StarterGear
		end
	end
end

-- Connect 'PromptGamePassPurchaseFinished' events to the 'onPromptGamePassPurchaseFinished()' function
MarketplaceService.PromptGamePassPurchaseFinished:Connect(onPromptGamePassPurchaseFinished)

Thanks

1 Like

Where do you contain this script?

1 Like

Serverscriptservice is where it is located.

GravityCoil is a variable with your coil’s ID and you trying to find it in ServerStorage. Above it is called JumpCoil.

1 Like
local MarketplaceService = game:GetService("MarketplaceService")

local GamePassIdTable = {
    ["SpeedCoil"] = 17930916,
    ["JumpCoil"] = 17930931,
    ["EpicSword"] = 18079239
}

function GiveGamePass(Player, GamePass)
    game.ServerStorage[GamePass]:Clone().Parent = Player.Backpack
    game.ServerStorage[GamePass]:Clone().Parent = Player.StarterGear
end

function OnPlayerAdded(Player)
    for GamePass, Id in pairs(GamePassIdTable) do
        if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, Id) then
            GiveGamePass(Player, GamePass)
        end
    end
end)

local function OnPromptGamePassPurchaseFinished(Player, PurchasedPassID, PurchaseSuccess)
    if PurchaseSuccess == true then
        for GamePass, Id in pairs(GamePassIdTable) do
            if PurchasedPassID == Id then
                GiveGamePass(Player, GamePass)
            end
        end
    end
end

game.players.playeradded:Connect(OnPlayerAdded)
MarketplaceService.PromptGamePassPurchaseFinished:Connect(OnPromptGamePassPurchaseFinished)

I assumed the instance was named JumpCoil, so if it was actually supposed to be GravityCoil change the string “JumpCoil” to “GravityCoil” in the table at the top of the script

anyways I tried to make the script more compact because it is in code review

also just a note I did use PascalCase for this so sorry, I should’ve made it camelCase like how you had it😅

1 Like