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

Why am I recieveing the error:attempt to index nil with 'WaitForChild' on line 7 ?

Asked by 3 years ago
Edited 3 years ago
game.Players.PlayerAdded:Connect(function(player)

    local plylist = {game.Players:GetPlayers()}

    if plylist[2] == nil then
        if player.CharacterAdded then
            player.Character:WaitForChild("Head")
            local memez = Instance.new("ParticleEmitter")
            memez.Parent = player.Character.Head
            memez.Name = "Memez"
        end
    end 
end)

Can someone please explain to me what is going wrong in this script and how I can fix? (It is not a local script)

0
Try getting used to using :GetService, for example: Instead of saying game.Players, say game:GetService(“Players”) RhysRocksRules 38 — 3y

1 answer

Log in to vote
0
Answered by
pwx 1581 Moderation Voter
3 years ago

Rather than player.CharacterAdded try this method:

local Players = game:GetService('Players')

Players.PlayerAdded:Connect(function(Player)
    local plyList = {Players:GetPlayers()}

    if plyList[2] == nil then
        -- it checks for character or waits for character
        local Character = Player.Character or Player.CharacterAdded:Wait()
        Character:WaitForChild('Head')
         local memez = Instance.new("ParticleEmitter")
                memez.Parent = Player.Character.Head
            memez.Name = "Memez"
    end -- plyList check
end) -- playerAdded
0
IT WORKED! That took forever for me to get working. Thank you! EvilWray 2 — 3y
0
No worries lad, have a good one. pwx 1581 — 3y
Ad

Answer this question