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

Why does it tell me .Character is "(a nil value)"?

Asked by
PastDays 108
4 years ago

Basically this script should continuously update the GUI of the Humanoids Health but instead it errors, I really can't figure out why...

Code:

wait(0.1)
while true do
    local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
    local hp = Humanoid.Health/100
    script.Parent:TweenSize(UDim2.new(hp,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,0.15)
    wait(.2)
end

Error Given:

Players.PastDays.Backpack.HoneyBadger.clientMain.mainGUI.HP.Frame.HealthDisplay:3: attempt to index field 'Character' (a nil value)

Any help appreciated!

0
because the character might not have been loaded when line 3 was ran theking48989987 2147 — 4y
0
you cant just have a wait(.01). that means wait for 1/100 of a second. you need to wait at least 1 second before running the script. AlphaWolf536791 28 — 4y
0
the best way to tell when it loads is by adding `player.CharacterAdded:Wait()` theking48989987 2147 — 4y
0
local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait() climethestair 1663 — 4y

4 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Please accept It is most likely that the users character has not loaded. To fix this problem, you can at the top add

This is probably the best way

repeat wait() until game.Players.LocalPlayer.Character

which will not let the script run until the player's character has been added. Another way to do this is by using a function:

function runLoop()
while true do
    local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
    local hp = Humanoid.Health/100
    script.Parent:TweenSize(UDim2.new(hp,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,0.15)
    wait(.2)
end
end

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(chr)
        runLoop()
    end)
end)
0
When i use this it doesn't error but also never loads. PastDays 108 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
wait(1)
while true do
    local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
    local hp = Humanoid.Health/100

script.Parent.Text = "Health: "..Humanoid.Health.."/100"

script.Parent:TweenSize(UDim2.new(hp,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,0.15)
    wait(.2)
end
Log in to vote
0
Answered by 4 years ago

Try adding in a variable for Character so that the character can load before the loop runs.

local Character = game.Players.LocalPlayer:CharacterAdded.Wait()
wait(0.1)
while true do
    local Humanoid = Character:WaitForChild("Humanoid")
    local hp = Humanoid.Health/100
    script.Parent:TweenSize(UDim2.new(hp,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,0.15)
    wait(.2)
end
Log in to vote
0
Answered by
OnaKat 444 Moderation Voter
4 years ago
Edited 4 years ago

Just wait until the character add.

And put it in while loop

Easy

while true do
    repeat wait() until game.Players.LocalPlayer.Character ~= nil
    local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
    local hp = Humanoid.Health/100
    script.Parent:TweenSize(UDim2.new(hp,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,0.15)
    wait(.2)
end

Answer this question