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

attempt to index nil with 'WaitForChild'??

For some reason, It says attempt to index nil with 'WaitForChild' Can somebody help me out?

game.Players.PlayerAdded:Connect(function(Player)
	Player.character:WaitForChild('Humanoid').WalkSpeed = Player.leaderstats.RunSpeed
end)

2 answers

✓ Accepted answer

The reason why this error is happening is because Player.character should be Player.Character. By using Player.character, character is nil, which is why you cannot use WaitForChild.

game.Players.PlayerAdded:Connect(function(Player)
    Player.Character:WaitForChild('Humanoid').WalkSpeed = Player.leaderstats.RunSpeed
end)

Also you should be checking to make sure the player's character exists so that this does not error, like so. But if the player's character does not exist when the player is added nothing will happen, which is what happens in most cases.

game.Players.PlayerAdded:Connect(function(Player)
	local char = Player.Character
	if char then
		Player.Character:WaitForChild('Humanoid').WalkSpeed = Player.leaderstats.RunSpeed
	end
end)

So the example below should help.

Alsoo, if you want the character's speed to always be edited even if they respawn, you should be using the CharacterAdded event of player like so

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(char)
		char:WaitForChild('Humanoid').WalkSpeed = Player.leaderstats.RunSpeed
	end)
end)

This way you no longer have to check if the character exists or not because the event only fires when a new one is added

2 comments

0

Actually, 'Player.character' still returns the player's Character. I tested it myself. It was simply the fact that the Character wasn't loaded in yet so it was nil. But yes, you should still capitalise the C.

0

Ah, I've been using roblox-ts for so long I've forgotten small inconsistencies like that xd

Player.character:WaitForChild('Humanoid').WalkSpeed = Player.leaderstats.RunSpeed

the second line on humanoid should be " " so use this script instead

Player.character:WaitForChild("Humanoid").WalkSpeed = Player.leaderstats.RunSpeed

5 comments

0

I still get "ServerScriptService.Script:2: attempt to index nil with 'WaitForChild'"

0

try

0

game.Players.PlayerAdded:Connect(function(player)

0

roblox is weird with capitlisation

0

Still doesnt work..

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