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)
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)
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
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
I still get "ServerScriptService.Script:2: attempt to index nil with 'WaitForChild'"
try
game.Players.PlayerAdded:Connect(function(player)
roblox is weird with capitlisation
Still doesnt work..
This discussion is preserved as it was. New answers, comments, and votes are closed.
2 comments
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.
Ah, I've been using roblox-ts for so long I've forgotten small inconsistencies like that xd