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

Problem with Health Display Script Not Sure How to Fix?

I am trying to make a health display script which seems to only work in studio but not in the actual game.

I get no errors at all in studio but in game it says "Players.Aepi.PlayerGui.StatsUI.HealthDisplay.Health.Health.Health:1: attempt to index field 'Character' (a nil value)"

To be honest i'm not sure what this error means, if someone could help that would be nice and thank you!

local Human = game.Players.LocalPlayer.Character.Humanoid

function update()
	local hpMath = Human.Health / Human.MaxHealth
	script.Parent.Size = UDim2.new(hpMath,0,1,0)
	script.Parent.Size = UDim2.new(hpMath,0,1,0)
	if Human.Health < Human.MaxHealth / 4 then
		script.Parent.BackgroundColor3 = Color3.new(255, 0, 0)
	elseif
		Human.Health > Human.MaxHealth / 4 then
		script.Parent.BackgroundColor3 = Color3.new(47/255, 255/255, 123/255)
	end
end

update()
Human.Changed:connect(update)

2 comments

0

This is a local script also!

0

It means the script is loading before the Character spawns.

1 answer

✓ Accepted answer

Just add in a simple repeat-until that waits for the Player and the Player's Character to load before executing the script

repeat wait() until game.Players.LocalPlayer.Character ~= nil
local Human = game.Players.LocalPlayer.Character.Humanoid

function update()
    local hpMath = Human.Health / Human.MaxHealth
    script.Parent.Size = UDim2.new(hpMath,0,1,0)
    script.Parent.Size = UDim2.new(hpMath,0,1,0)
    if Human.Health < Human.MaxHealth / 4 then
        script.Parent.BackgroundColor3 = Color3.new(255, 0, 0)
    elseif
        Human.Health > Human.MaxHealth / 4 then
        script.Parent.BackgroundColor3 = Color3.new(47/255, 255/255, 123/255)
    end
end

update()
Human.Changed:connect(update)

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