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
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.
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