ServerScriptService.Script:5: attempt to index nil with 'Humanoid'

I’m making a script where every second your speed goes up but I keep getting thrown the Error in the title Anyone know why?

(This is in a server script Btw)

You should not do that. If you do that, that script will only run on a single player. And you forgot to add wait(1) at the end too. Let me explain what you gotta do:

First, add these into your explorer.

resim_2024-04-01_142325895

Then, open the script inside serverscriptservice and paste this:

game.ReplicatedStorage.ChangePlrWalkspeed.OnServerEvent:Connect(function(plr)
plr.Character:WaitForChild(“Humanoid”).WalkSpeed += 1
end)

Now, open the local script in startercharacterscripts and paste this:

while true do
wait(1)
game.ReplicatedStorage:WaitForChild(“ChangePlrWalkspeed”):FireServer()
end

And youre done! Tell me if it works or not.

Don’t need to pass player. It’s passed automatically. Also, the current code won’t only run for one player.

You don’t even need to use a remote.

Change the code to this:

Players.PlayerAdded:Connect(function(player)
    local char = player.Character or player.CharacterAdded:Wait()
    local hum = char:WaitForChild("Humanoid")
    while task.wait(1) do
        hum.WalkSpeed += 1
    end
end)

OR:

LocalScript in StarterCharacterScripts:

while true do
    script.Parent:WaitForChild("Humanoid").WalkSpeed += 1
    task.wait(1)
end

Just tested it, didnt work. (added it in serverscriptservice as a normal script not local)

I made a typo there and fixed it now.

still doesnt work. character limit

This, inside a Script in ServerScriptService, is tested and works successfully.

game.Players.PlayerAdded:Connect(function(player)
	local char = player.Character or player.CharacterAdded:Wait()
    local hum = char:WaitForChild("Humanoid")
	while true do
		hum.WalkSpeed += 1
		task.wait(1)
	end
end)

No errors, fully tested.

(All of them were meant to be a server script in ServerScriptService, you probably put it in wrong).

1 Like

I appreciate your help but what does “player.characterAdded:Wait()” do? I’m seeing this a lot in scripts and I want to know how to use it

The :Wait() function yields the code until the signal fires, returning any arguements it gives.

Basically:
player.CharacterAdded is an event that fires when a player’s character spawns in. It also gives one parameter: the character itself.

The or statement will check to see if the first statement is nil or false. If it is, it will then check the second arguement. This means, if the player’s character hasn’t loaded yet, it will wait until it has loaded, and then continue with the code, using that character.

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

Statement One:
player.Character being a property of the player used to reference their character.

or:
Statement Two will only run if Statement One comes out as false or nil. It can be used for other comparisons thanks to Boolean Logic.

Statement Two:
(Only runs if character has not loaded yet).

player.CharacterAdded fires and gives the player’s character as a parameter. By waiting for this with :Wait(), we are yielding the code and then storing the character in our variable. Either way, the variable comes out to be the player’s character. It’s just a lot easier than doing conditional loops, repeats until the character is there.

:Wait() can be used on any event, not just this one.

Dont use this code, repeating :WaitForChild() Indefinitely will slow down your game.

2 Likes

A server script

local players = game:GetService("Players")
players.PlayerAdded:Connect(function(plr)
 local character = plr.Character or plr.CharacterAdded:Wait()
 while task.wait(1) do
character.Humanoid.WalkSpeed += 1
 end
end)

Yeah, forgot to take that bit out. I wrote that incredibly fast and was kinda busy, definitely should only use WaitForChild once outside of the loop. I updated the post now

Thanks for pointing it out :slight_smile: .

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.