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 4 years ago

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

Log in to vote
1
Answered by
DanzLua 2879 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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

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. xInfinityBear 1777 — 4y
0
Ah, I've been using roblox-ts for so long I've forgotten small inconsistencies like that xd DanzLua 2879 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

0
I still get "ServerScriptService.Script:2: attempt to index nil with 'WaitForChild'" Galaxybombboy 134 — 4y
0
try GameStealerKid 79 — 4y
0
game.Players.PlayerAdded:Connect(function(player) GameStealerKid 79 — 4y
0
roblox is weird with capitlisation GameStealerKid 79 — 4y
0
Still doesnt work.. Galaxybombboy 134 — 4y

Answer this question