Error with find first child

I am trying to make a speed simulator, but sometimes, especially when the player takes a long time to load, The steps don’t increase. The error is right under the if character:FindFirstChild("Humanoid") then I don’t understand. It checks and finds the humanoid, but then it says there is not. Please help me.

Error - ServerScriptService.MainLoop1386216709:20: attempt to index nil with ‘FindFirstChild’

script.


task.wait()
local userIdString = string.sub(script.Name, 9)
local userId = tonumber(userIdString)
local player = game.Players:GetPlayerByUserId(userId)
local character = player.Character

local leaderstats = player:WaitForChild("leaderstats")
local steps = leaderstats.Steps.Value
local speed = leaderstats.Speed.Value
local points = leaderstats.Points.Value
local trail = player.PlayerTrail.Value

local multiplier = player.Multiplier.Value
local mult = multiplier/10

while true do
	
	wait(1)
	if character:FindFirstChild("Humanoid") then
		
		
local humanoid = character.Humanoid
humanoid.WalkSpeed = speed
humanoid.JumpPower = speed * .75
end

you’re using an old character model. Which means when a player dies, the Character will be nil. To fix this, we can do

task.wait()
local userIdString = string.sub(script.Name, 9)
local userId = tonumber(userIdString)
local player = game.Players:GetPlayerByUserId(userId)


local leaderstats = player:WaitForChild("leaderstats")
local steps = leaderstats.Steps.Value
local speed = leaderstats.Speed.Value
local points = leaderstats.Points.Value
local trail = player.PlayerTrail.Value

local multiplier = player.Multiplier.Value
local mult = multiplier/10

while true do
	local character = player.Character
	if character then
		if character:FindFirstChildWhichIsA("Humanoid") then


			local humanoid = character.Humanoid
			humanoid.WalkSpeed = speed
			humanoid.JumpPower = speed * .75
		end
	
	end
	task.wait(1)
end

I think its because in the loop the chars not loaded idrk

Okay I will try this. Thanks. I will not solution this yet because this might not work. I it is hard to test and load character slowly, because my computer is fast.

Instead of using :FindFirstChild(), use :WaitForChild().

I think he is right, i think you can use

local character = player.Character or player.CharacterAdded:Wait()

so you are guaranteed to get the players character

That wont work after the player dies. You can do player.characteradded but in this case it doesn’t seem like a good option

you’re missing and end (in case it’s the entire code

other than that, you should use this:

player.CharacterAdded:Connect(function (character)
if character:FindFirstChildWhichIsA("Humanoid") then
local humanoid = character.Humanoid
while true do
humanoid.WalkSpeed = speed
humanoid.JumpPower = speed * .75
task.wait(1)
end
end
end)

(sorry if I’m missing anything and its not formatted because im writing this on mobile

local players = game:GetService("Players")

local userIdString = script.Name:sub(9)
local userId = tonumber(userIdString)
local player = players:GetPlayerByUserId(userId)

local leaderstats = player:WaitForChild("leaderstats")
local steps = leaderstats.Steps.Value
local speed = leaderstats.Speed.Value
local points = leaderstats.Points.Value
local trail = player.PlayerTrail.Value
local multiplier = player.Multiplier.Value
local mult = multiplier/10

while true do
	task.wait(1)
	local character = player.Character
	local humanoid = character:FinFirstChild("Humanoid")
	if humanoid then
		if humanoid.Health > 0 then
			humanoid.WalkSpeed = speed
			humanoid.JumpPower = speed * 0.75
		end
	end
end

I believe this works now. Thank You.