Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited by JesseSong 4 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

Hi I have a problem, please help me.

01local Power = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Power")
02local Char =  game.Players.LocalPlayer.Character
03Char:WaitForChild("Humanoid")
04 
05local function Set(Regen)
06    if Regen == true then
07        game.ReplicatedStorage.SetHealth:FireServer(Char, math.floor(100 + Power.Value/10), math.floor(100 + Power.Value/10))
08    else
09        game.ReplicatedStorage.SetHealth:FireServer(Char,nil, math.floor(100 + Power.Value/10))
10    end
11 
12end
13 
14Power.Changed:Connect(function()
15    Set(false)
View all 21 lines...

OUTPUT:

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

0
This a script or LocalScript? NiniBlackJackQc 1562 — 4y
0
Local script since he used LocalPlayer Desmondo1 121 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago

You added Char. Remove it and it should work fine

1local Char = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
0
where did i remove it batmath62 5 — 4y
0
this doesn't work batmath62 5 — 4y
Ad
Log in to vote
0
Answered by
0hsa 193
4 years ago

first make the character variable:

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

and replace the character:waitforchild with:

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

or:

1repeat wait() until Char:findFirstChildOfClass("Humanoid")
Log in to vote
0
Answered by
Desmondo1 121
4 years ago
Edited 4 years ago

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

1Char:WaitForChild("Humanoid")

Instead, add "local Humanoid = " behind it.

1local Humanoid = Char:WaitForChild("Humanoid")

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

1game.Loaded:Wait()

does that.

Answer this question