Attempt to index nil with Walk Speed

Hello. I’m getting the error *attempt to index nil with WalkSpeed * coming from line 16 of this server script

the code

local SpecialParts = game.Workspace.SpecialParts

for i, part in pairs(SpecialParts:GetChildren()) do
	local db = false

	local Speed = part:GetAttribute("WalkSpeed")

	if part.Name == "Speedbrick" then

		part.Touched:Connect(function(Hit)
			if not db then
				db = true

				local Humanoid = Hit.Parent:findFirstChild("Humanoid")

				Humanoid.WalkSpeed += Speed

				task.wait(0.1)

				db = false
			end
		end)
	end
end

1 Like

I’m pretty sure the script isn’t checking if the hit is a character or some random part. In order to do this, you have to add an if statement.

local SpecialParts = game.Workspace.SpecialParts

for i, part in pairs(SpecialParts:GetChildren()) do
	local db = false
	local Speed = part:GetAttribute("WalkSpeed")
	if part.Name == "Speedbrick" then
		part.Touched:Connect(function(Hit)
			if Hit.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(Hit.Parent) then
				if not db then
					db = true
					local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
					Humanoid.WalkSpeed += Speed
					task.wait(0.1)
					db = false
				end
			end
		end)
	end
end

Check if this works.

1 Like

Make sure you capitalize the first F in FindFirstChild.

The script won’t find your humanoid, and return a nil when attempting to index the value.

4 Likes