Attempt to index nil with 'Head'

I’m attempting to learn magnitude

while wait() do
	for _,v in pairs(game.Players:GetChildren()) do
		if (v.Character.Head.Position - script.Parent.Position).Magnitude <= 10 then
			script.Parent.BrickColor ="Really red")
			script.Parent.BrickColor ="Really black")

The error: [ Workspace.MainPart.Script:3: attempt to index nil with ‘Head’]

The player’s character doesn’t exist when the script references it. The solution is to just wait for the character to load. Above the error line, put this line above it:

local char = v.Character or v.CharacterAdded:Wait()
1 Like

So i have to state what the character is in a variable, why does this change to putting it in without a variable, Could you explain please?

v.Character or v.CharacterAdded:Wait()

I’m sure this line will error, but I’m not certain of it.

1 Like

It worked :slight_smile: Thank you

30 chararararar