Attempt to index nil with 'Character' Error

So this VIP door was working fine until the other day when it started giving me an error.
2. What is the issue? Include screenshots / videos if possible!

I always get stuck on this stuff but so far I have tried calling

local player = game.Players.LocalPlayer

However that didn’t work.

The Error/Error Line (Serverside Script):

local character = player.Character

Workspace.VIPdoor.Script:22: attempt to index nil with ‘Character’

Full Code:

local GamepassId = 12424604
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, GamepassId)
	if allow then
		open()
		delay(2, close)
	end
end)

I know this is a simple error, but I’m stumped.

3 Likes

Add a wait to the start. Is the script local?

1 Like

No it is server-sided. Sorry for the confusion.

Your good! :smiley: Whats the error say?

Well, you do have a guard clause, but it’s not placed in a useful spot. You assume player is not nil, and try to access the player’s character. So you should move that guard up one line.

You also being verbose in how you search for the player.

local player = game.Players:GetPlayerFromCharacter(part.Parent)
if not player then return end
-- player is not nil

Why doesn’t he just add a wait for the character to load in? Thats the issue you know…

Just add a wait to the start. Also put a line that says if player == nil then print(“Nil”) end

I am not incorrect. If a collision were to happen that were not under a player’s character (say, an NPC) it will error…

Simple fix.

local char = game.Players.LocalPlayer.Character
repeat wait() until char

He said its a server script so he cant do game.Players.LocalPlayer thats why its nil.

He said it as an example, not that he’s using it right now. It was his attempt to fix it.

Still he said its a server script so localplayer will be nil causing the character to be nonexistent.

Tried your suggestion didn’t work. Unless I was not suppose to put it at the beginning of the script.

Try to add player into your function and using that instead of localplayer.

Change this to:

door.Touched:Connect(function(hit)
    if game.Players:GetPlayerFromCharacter(hit.Parent) then
        — do your stuffs here.
    end
end)
door.Touched:Connect(function(part)
	local player = game.Players:GetPlayerFromCharacter(part.Parent)
	if not player then return end
	local character = player.Character
	local allow = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, GamepassId)
	if allow then
		open()
		delay(2, close)
	end
end)
1 Like

Ayee you stole my idea XDD!! Lol jk lol

That fixed it it no longer displays the error and works perfectly!
Also gotta credit @BMWLux for the idea lol.

Lol thank you!! Ik im such a smart lil boi

1 Like