Teleport failing because attempting to index nil with UserId

I’m making a gui that teleports the player to another place after clicking on a gui and owning a gamepass. It’s raising an error:

Error while checking if player has pass: Players.Roblozinko.PlayerGui.TeleportGui.TeleportButton.LocalScript:11: attempt to index nil with ‘UserId’ (x3)

here’s my script.

local teleportService = game:GetService("TeleportService")
local gamepassService = game:GetService("GamePassService")
local players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")

local gamepassID = 13702484

local function guiClick(player)
	local hasPass = false
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassID)
	end)
	if not success then
		warn("Error while checking if player has pass: " .. tostring(message))
		return
	end
	if hasPass == true then
		print(player.Name .. "owns the gamepass:" .. gamepassID)
		--teleport
		teleportService:Teleport(6167832809, player)
	else
		--player does not own the gamepass
		MarketplaceService:PromptGamePassPurchase(player, gamepassID)
	end
end

script.Parent.MouseButton1Click:Connect(guiClick)

I mixed a couple of tutorials from Developer Hub together to see if it would work, if that helps.

Thank you for your answers.

MouseButton1Click does not return the player, if it’s a Localscript then you can just use game.Players.LocalPlayer, if it’s a Script then you need to find another way to get the player

Could you tell me a way I could do that?

Anytime you’re using GUIs you need to use a LocalScript, not always required but it’s always reccomended.
So, you could just use the MouseButton1Click and then fire a RemoteEvent to the server, where the player will be teleported.
RemoteEvents fired to the server always have the first argument as a player who fired, therefore giving you a valid PlayerId

If you wanna do it in the local script just add this line and replace “player” with "LocalPlayer " and remove the “player” parameter from guiClick function (However I would recommend you doing this check on server)

local LocalPlayer =  players.LocalPlayer