Attempt to index nil with 'Humanoid'

Hi Roblox! i’m trying to Create a system for my new game where when you click with a tool you get bigger. I’ve encountered this error when testing, does anyone know ho to fix it?

ServerScriptService.Script:4: attempt to index nil with 'Humanoid’

Code:

while wait(0.5) do
	local children = game.Players:GetChildren()
	for i = 1, #children do
		local humanoid = children[i].Character.Humanoid
		humanoid.HeadScale.Value = children[i].leaderstats.Pies.Value/50 +5
		humanoid.BodyHeightScale.Value = children[i].leaderstats.Pies.Value/50 +5
		humanoid.BodyWidthScale.Value = children[i].leaderstats.Pies.Value/50 +5
		humanoid.BodyDepthScale.Value = children[i].leaderstats.Pies.Value/50 +5
	end
end

Thanks!

1 Like

Hello,

This issue seems to be because you are assuming the player has a character loaded, or that the characters humanoid has not been loaded at the time you are defining it.

I personally would add a statement above all the humanoid modifying code saying something along the lines off.

if(not player.Character or not player.Character:FindFirstChild(“Humanoid”) then return end

1 Like

Where player is replaced with children[i]

1 Like

yes, i would also recommend using an if statement or something to see if the players character exists:

if children[i].Character then
  -- ...
end
1 Like

Hi Pyxrien,

thanks for your help. however i don’t know where i should put this(the end part) do you mind adding it into the code for me? it would really help, thanks!

1 Like

Yes, sorry for the slow response, busy packing for a trip tomorrow!

while wait(0.5) do
	local children = game.Players:GetChildren()
	for i = 1, #children do
        if(not children[i].Character or not children[i].Character.Humanoid) then continue end
		local humanoid = children[i].Character.Humanoid
		humanoid.HeadScale.Value = children[i].leaderstats.Pies.Value/50 +5
		humanoid.BodyHeightScale.Value = children[i].leaderstats.Pies.Value/50 +5
		humanoid.BodyWidthScale.Value = children[i].leaderstats.Pies.Value/50 +5
		humanoid.BodyDepthScale.Value = children[i].leaderstats.Pies.Value/50 +5
	end
end

Give that a shot, and let me know if it works or not! :slight_smile:

replaced return with continue as I forgot that return will stop the loop entirely…

Small tip use GetPlayers over GetChildren as it will only even pick up players.

2 Likes

I am not sure if you are wanting to inc the heigh of all players humanoids or just the player using the tool.

If you just want to only effect the player holding the tool I would look at using a remote event which has the player passed as the first argument so no loops needed.

What if the player’s character doesn’t exist, or has not loaded yet? We can toss in a few if statements to make sure everything is in order. We can do this:

while wait(0.5) do
	local children = game.Players:GetPlayers()
	for index = 1, #children do
		local character = children[index].Character
		if character then
			local humanoid = character:FindFirstChild("Humanoid")
			if humanoid then
				local value = children[index].leaderstats.Pies.Value / 50 + 5
				humanoid.HeadScale.Value = value 
				humanoid.BodyHeightScale.Value = value 
				humanoid.BodyWidthScale.Value = value 
				humanoid.BodyDepthScale.Value = value 
			end
		end
	end
end

What we’re doing is that we’re checking for a character, and if the character exists, we’ll look at the character’s humanoid. If they’re fully loaded in, we can execute the code.

Edit: Also, I recommend create a single variable for the value that the humanoid can use.

pretty much what i just said but yeah

Opps I thought I was quoted @Scxiptifyz. Sorry about that.

there are a few things wrong here.

  1. use task.wait() instead of wait(), it is much better.
  2. you should check if Humanoid or character has loaded yet
  3. you should use game:GetService(service) for services (Players is a service)
  4. in this scenario, use :GetPlayers instead of GetChildren.

here is the fixed script

while true do
    local children = game:GetService("Players"):GetPlayers()
    for i, v in ipairs(children) do -- yes i changed this loop cuz i like to use this more, still works the same
        local lstats = v.leaderstats
        local chr = v.Character or v.CharacterAdded:Wait() -- in case character ain't loaded in
        local humanoid = chr:WaitForChild("Humanoid")
        humanoid.HeadScale.Value = (lstats.Pies.Value / 50) + 5
        humanoid.BodyHeightScale.Value = (lstats.Pies.Value / 50) + 5
        humanoid.BodyWidthScale.Value = (lstats.Pies.Value / 50) + 5
        humanoid.BodyDepthScale.Value = (lstats.Pies.Value / 50) + 5
    end
    task.wait(0.5)
end

I also saw other flaws, so here is my improved version

local PS = game:GetService("Players")
PS.PlayerAdded:Connect(function(plr) -- uses function everytime a player joins
    local pies = plr:WaitForChild("leaderstats"):WaitForChild("Pies")
    plr.CharacterAdded:Connect(function(chr)  -- uses function everytime the player's character loads
        local hum = chr:WaitForChild("Humanoid") -- updates the scaling and stuff
        hum.HeadScale.Value = (pies.Value/50) + 5
        hum.BodyHeightScale.Value = (pies.Value/50) + 5
        hum.BodyWidthScale.Value = (pies.Value/50) + 5
        hum.BodyDepthScale.Value = (pies.Value/50) + 5
        pies:GetPropertyChangedSignal("Value"):Connect(function() -- updates the scaling and stuff everytime the pies' value changes
            hum.HeadScale.Value = (pies.Value/50) + 5
            hum.BodyHeightScale.Value = (pies.Value/50) + 5
            hum.BodyWidthScale.Value = (pies.Value/50) + 5
            hum.BodyDepthScale.Value = (pies.Value/50) + 5
        end)
    end)
end)

--[[
    improvements: no unnecessary polling, updates the scale stuff quicker.
]]--