Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does attempt to index field 'Character' (a nil value) pop up?

This is the code:

    while wait(0.5) do

local players = game.Players:GetChildren()

for i, plr in ipairs(players) do

if plr:FindFirstChild("InSafezone") then

local shieldCheck = plr.Character:FindFirstChildOfClass("ForceField")

if plr.InSafezone.Value == true then

if not shieldCheck then

local shield = Instance.new("ForceField")

shield.Parent = plr.Character

end

else

if shieldCheck then

shieldCheck:Destroy()

end

end

end

end

end

1 answer

✓ Accepted answer

To debunk the error, make a character variable:

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

This does 1 of 2 things:

A. If the Character has already spawned in, move on to the next line.

B. If the Character is loading in but has not spawned in, wait for the character to spawn before moving onto the next line.

This should fix your problem.

1 comment

0

<3

This discussion is preserved as it was. New answers, comments, and votes are closed.