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

local gamePassID = 11522601  

local function onPlayerAdded(player)
	
	local hasPass = false
	
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
	end)
	if hasPass == true then
local part = script.Parent
local detector = part.ClickDetector
local weapon = game.Lighting.VIP
local debounce = false

detector.MouseClick:connect(function(player) 
	if debounce == false then
		local g = weapon:Clone()
		g.Parent = player.Backpack
		debounce = true
		wait(2)
		debounce = false
				if haspass == false
					then
					game:GetService("MarketplaceService"):PromptPurchase(game.Players.LocalPlayer,11522601 )
				end
	end
end)
	
		

Basically what this script is supposed to do is when a player clicks the ''VIP'' item it clones the item in the players inventory, if the player does not have the gamepass it prompts the purchase.

I have tested this script through various stages and it worked up until I added the gamepass-only function I don't know if I'm going wrong on multiple points, but the main problem seems to be when I'm closing the functions. The last ''end)'' and I keep getting the same error:

Expected 'end' (to close then on line 27) but got .

Does anyone know where I'm going wrong?