Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Attempt to index nil with WaitForChild?

Asked by
RFL890 4
4 years ago

So here's my code. It's for a spawner.

-- Locals
local team = script.Parent:WaitForChild("Team").Value
local root = script.Parent.Location:WaitForChild("HumanoidRootPart")
local root2 = script.Parent.Parent.Spawn2.Location:WaitForChild("HumanoidRootPart")
local team1 = game:GetService("Teams"):WaitForChild("Assault")
local team2 = game:GetService("Teams"):WaitForChild("Defend")

-- Setup
for _, v in pairs(script.Parent:GetDescendants()) do
    if v:IsA("BasePart") then v.Anchored = true v.CanCollide = false end
end

for _, v in pairs(script.Parent.Location:GetDescendants()) do
    if v:IsA("BasePart") then
        v.Transparency = 1
        v.CanCollide = false
        v.Anchored = true
    end
end

-- Main
game:GetService("Players").PlayerAdded:Connect(function(plr)
    for i, v in pairs(game:GetService("Players"):GetChildren()) do
        if i < 1 then
            plr.Team = team1
        elseif i > 1 then
            plr.Team = team2
        end
    end 
    if plr.Team == team1 then
        plr.Character:WaitForChild("HumanoidRootPart").CFrame = root.CFrame
    elseif plr.Team == team2 then
        plr.Character:WaitForChild("HumanoidRootPart").CFrame = root2.CFrame
    end 
end) 


Please don't say "Use a spawnlocation" because i'm just testing this. So i keep getting this error: Workspace.Spawn.Script:31: attempt to index nil with 'WaitForChild'  -  Server  -  Script:31 How can i fix it?

3 answers

Log in to vote
0
Answered by 4 years ago

It means that the character does not exist, as the player just joined the game. Try waiting for their character to load before moving it.

game:GetService("Players").PlayerAdded:Connect(function(plr)
    for i, v in pairs(game:GetService("Players"):GetChildren()) do
        if i < 1 then
            plr.Team = team1
        elseif i > 1 then
            plr.Team = team2
        end
    end
    local character = plr.Character or plr.CharacterAdded:Wait() 
    if plr.Team == team1 then
        character:WaitForChild("HumanoidRootPart").CFrame = root.CFrame
    elseif plr.Team == team2 then
        character:WaitForChild("HumanoidRootPart").CFrame = root2.CFrame
    end 
end) 

Ad
Log in to vote
0
Answered by 4 years ago

I believe this means that the HumanoidRootPart is nil, and does not exist in plr.Character.

Log in to vote
0
Answered by
R_alatch 394 Moderation Voter
4 years ago
Edited 4 years ago

Wrap the if statement inside of a CharacterAdded event to avoid getting an error if the player's Character hasn't loaded yet.

I also recommend using the player's PrimaryPart instead of the HumanoidRootPart because of useful functions such as SetPrimaryPartCFrame. The player's primary part is always their HumanoidRootPart which works for R6 and R15.

plr.CharacterAdded:Connect(function(character)
    plr.Team == team1 then
        plr.Character:SetPrimaryPartCFrame(root.CFrame)
    elseif plr.Team == team2 then
        plr.Character:SetPrimaryPartCFrame(root2.CFrame)
    end
end

Answer this question