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 3 years ago
Edited by JesseSong 3 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.

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'

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

3 answers

Log in to vote
0
Answered by 3 years ago

You added Char. Remove it and it should work fine

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

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")
Log in to vote
0
Answered by
Desmondo1 121
3 years ago
Edited 3 years ago

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.

Answer this question