Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

PlayerScripts.HealthUp:3: attempt to index nil with 'WaitForChild' how to fix that ?

Hi I have a problem, please help me.

local Power = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Power")
local Char =  game.Players.LocalPlayer.Character
Char:WaitForChild("Humanoid")

local function Set(Regen)
	if Regen == true then
		game.ReplicatedStorage.SetHealth:FireServer(Char, math.floor(100 + Power.Value/10), math.floor(100 + Power.Value/10))
	else
		game.ReplicatedStorage.SetHealth:FireServer(Char,nil, math.floor(100 + Power.Value/10))
	end
	
end

Power.Changed:Connect(function()
	Set(false)
end)

Char.Humanoid.Died:Connect(function()
	game.Players.LocalPlayer.CharacterAdded:Wait()
	Set(true)
end)


##OUTPUT:

PlayerScripts.HealthUp:3: attempt to index nil with 'WaitForChild'

2 comments

0

This a script or LocalScript?

0

Local script since he used LocalPlayer

3 answers

You added Char. Remove it and it should work fine

local Char = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

2 comments

0

where did i remove it

0

this doesn't work

first make the character variable:

local Char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:wait()

and replace the character:waitforchild with:

local humanoid = Char:waitForChild("Humanoid",10)

or:

repeat wait() until Char:findFirstChildOfClass("Humanoid")

If you want a variable for the humanoid. Don't just do

Char:WaitForChild("Humanoid")

Instead, add "local Humanoid = " behind it.

local Humanoid = Char:WaitForChild("Humanoid")

If you want the script to wait until the player is loaded, I'm pretty sure

game.Loaded:Wait()

does that.

This discussion is preserved as it was. New answers, comments, and votes are closed.