Attempt to index nil with "CharacterAdded", not detecting the character

This is really frustrating, what’s wrong with this code?

local Player = game:GetService("Players").LocalPlayer;
local Character = Player.Character or Player.CharacterAdded:Wait();
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart");

Mainly errors on the Character variable, not sure if the HumanoidRootPart variable is correct either.

(Local Script)

2 Likes

Is this on a serverscript because otherwise this error wouldn’t make sense.

And if it is on a serverscript, you can’t use Players.LocalPlayer on a serverscript because it can be only used by localscripts.

1 Like

Yeah, I know that. I’m experienced. It’s in a local script. Truly do not understand why it errors.

After the CharacterAdded event do repeat wait() until Character:FindFirstChild(“HumanoidRootPart”) or replace the event with it Player | Documentation - Roblox Creator Hub

1 Like

I’ve tried both changing it and doing repeat wait() until game.Players.LocalPlayer.CharacterAdded:Wait();, still errors with the same thing.

I meant doing something like this

local Player = game:GetService("Players").LocalPlayer;
local Character = Player.Character or Player.CharacterAdded:Wait();
repeat wait() until Character:FindFirstChild("HumanoidRootPart")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart");
2 Likes

Can I ask what is the point of using repeat wait() until Character:FindFirstChild("HumanoidRootPart") if you’re going to use Character:WaitForChild("HumanoidRootPart") in the next line?

Just added the repeat wait line to his script, no point really but he can change it if he wants