Attempt to index nil with "Character"

So I keep getting this error saying “Workspace.Player1.LocalScript:4: attempt to index nil with ‘Character’”. THe script is in a local script inside StarterCharacterScripts and the way I tried getting the character is

local plr = game.Players.LocalPlayer
local character = plr.Character

What is wrong?

Try this

local character = plr.Character or plr.CharacterAdded:Wait()

Basically the problem is you are loading the script is loading faster than the game.

1 Like

Don’t thing that’s it. I still keep getting the same error

Try this,

local plr = game.Players.LocalPlayer
plr.CharacterAdded:Connect(function(char)
-- code
end)
1 Like

Is this a Server script? If so change it local script and Place it inside of StarterCharacterScripts
Add this code

local plr = game.Players.LocalPlayer
local character = script.Parent
1 Like

If it’s in StarterCharacterScripts then you can simply do

local character = script.Parent

I have no clue why but this still isn’t working… I’ve tried all of this before asking and they didn’t seem to work.

Can you add a LOCAL Script inside of StarterCharacterScripts?
As I’ve said earlier?

local plr = game.Players.LocalPlayer
local character = script.Parent

Can you send the full script? This would be helpful.

It’s already in a local script?

1 Like
local plr = game.Players.LocalPlayer
local char = plr.Character

local head = char:WaitForChild("Head")

local event = game.ReplicatedStorage:WaitForChild("LocalPlayer")

event.OnClientEvent:Connect(function(player)
	local headFolder = player.Character:WaitForChild("headContainer")
end)

Did you add this code??

local plr = game.Players.LocalPlayer
local character = script.Parent

What’s the point or referencing the player if I’m not going to use it? And also it doesn’t work like I’ve said before

Try this

local Character = script.Parent

local head = Character:WaitForChild("Head")

local event = game.ReplicatedStorage:WaitForChild("LocalPlayer")

event.OnClientEvent:Connect(function(player)
	local headFolder = Character:WaitForChild("headContainer")
end)

It Doesn’t Work… That’s why I’m asking

local player = game:GetService('Players').LocalPlayer
local char = script.Parent

local head = char:WaitForChild('Head')

local event = game:GetService('ReplicatedStorage'):WaitForChild('LocalPlayer')

event.OnClientEvent:Connect(function(targetPlayer)
	if targetPlayer.Character ~= nil then
		local headFolder = targetPlayer.Character:WaitForChild('headContainer')
	end
end)

For some strange reason, after deleting the entire thing and rewriting it the exact same way, it worked…?

local plr = game.Players.LocalPlayer
local char = plr.Character

I used this before and it didn’t work and when I tried again it worked. Could this be a bug or something?

1 Like

This problem is probably because the script is running faster than the character can load in, so the script error’s when looking for the player’s character because the character does not exist yet.

I tried waiting for the character to load then for the script to run but it still didn’t work

I’m assuming this is all being run in a local script. Given that, where is this local script located (ie, StarterPlayerScripts, StarterCharacterScripts)?

1 Like