Anyone know what is wrong with this script?

I am making a vip door for my game, Dayron Grill and was wondering if someone could help me figure this out.

local door = script.Parent

function open()
	door.CanCollide = false
end

function close()
	door.CanCollide = true
end

function get_player(part)
	for _, player in ipairs(game.Players:GetPlayers()) do
		if part:IsDescendantOf(player.Character)then
			return player
		end
	end
end

door.Touched:Connect(function(part)
	local player = get_player(part)
	local character = player.Character
	if not player then return end
end)

local allow = (
	game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.userId, 12424604)

)

if allow then
	open()
	delay(1, close)
end

That is the script, the line it is giving me an error at is

	game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.userId, 12424604)

(Line 26)

It is an UnknownGlobal Error

I believe its player.UserId not player.userId

You didn’t define the player correctly. When you use local this = that inside of a function you can only use that info in that function. You have your UserOwnsGamepassAsync outside of that function so the script doesn’t know what player is. You will need to put it all under one function of defining the player in a different way.

EDIT:
Also change userId to UserId like @Ze_tsu said.

1 Like

I got this out of an outdated tutorial, how do i do that? Also, I changed it to UserId dont worry.

This is false. Either method is the same.

Also, to resolve the question. The player variable is locally defined within the function, meaning it is not applied globally within the script. Therefore, you will want to define a global variable outside the function. This can be denoted as: player = nil at the start of the script, or whatever you prefer to name it.

Yeah, I just checked it and you’re right. Thank you for correcting me though!

So do i just take the

local

out?

This should work:

Let me know if you get any errors.

local door = script.Parent

function open()
	door.CanCollide = false
end

function close()
	door.CanCollide = true
end

function get_player(part)
	for _, player in ipairs(game.Players:GetPlayers()) do
		if part:IsDescendantOf(player.Character)then
			return player
		end
	end
end

door.Touched:Connect(function(part)
	local player = get_player(part)
	local character = player.Character
	if not player then return end
	local allow = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, 12424604)
	if allow then
		open()
		delay(1, close)
	end
end)

Edit: Use @xH_xyden’s code.
30char

No errors! Almost forgot to buy my Game Pass to make it easier to travel.