Why am I getting an 'Attempting to index nil with HumanoidRootPart' error?

  1. What do you want to achieve? A script that increases a player’s speed

  2. What is the issue? I’m getting an error that says that I’m attempting to index nil with HumanoidRootPart thought I’m not sure why it’s happening

here’s my script, all help is appreciated

local leaderstats = script.Parent.leaderstats

game.Players.PlayerAdded:Connect(function(player)
	
	local hrp = player.Character.HumanoidRootPart
	local startPosition = hrp.CFrame.Position
	local distance = (startPosition - hrp.CFrame.Position).Magnitute
	
	while true do
		if distance > 1 then

			player.Character.Humanoid.WalkSpeed += 1
			leaderstats.Speed.Value += 1
		
		end
	end
end)

try to add a :waitForChild() sometimes the part doesnt load in fully

local hrp = player.Character:WaitForChild("HumanoidRootPart")
1 Like

now its saying that I’m attempting to index nil with ‘WaitForChild’

This happens because the PlayerAdded event runs before the character can load, try this:

local leaderstats = script.Parent.leaderstats

game.Players.PlayerAdded:Connect(function(player)
	repeat task.wait() until player.Character
	local hrp = player.Character.HumanoidRootPart
	local startPosition = hrp.CFrame.Position
	local distance = (startPosition - hrp.CFrame.Position).Magnitute

	while true do
		if distance > 1 then

			player.Character.Humanoid.WalkSpeed += 1
			leaderstats.Speed.Value += 1

		end
	end
end)

Since PlayerAdded is fired, no character was made prior to it. The only way a character is made is when CharacterAdded is fired, an event of player.