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

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

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)

1 comment

0

Try getting used to using :GetService, for example: Instead of saying game.Players, say game:GetService(“Players”)

1 answer

✓ Accepted answer

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

2 comments

0

IT WORKED! That took forever for me to get working. Thank you!

0

No worries lad, have a good one.

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