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

Scripts are not reading health right?

I have turned the health coregui off and am trying to make my own gui, but when I run it it displays a bunch of random numbers.

health lower script:

local healthValue = 100

while healthValue > 0 do
	healthValue = healthValue - 5
game.Players.LocalPlayer.Character.Humanoid.Health = healthValue
wait(5)
end

health reading script:

local textValue = script.Parent
local healthValue = game.Players.LocalPlayer.Character.Humanoid
local value = 0
while true do
	value = healthValue.Health
textValue.Text = (value)	
	wait (1)
end

1 answer

✓ Accepted answer

I'm not sure why that's displaying random numbers, but your health reading script is woefully inefficient.

Try this code instead, using the HealthChanged Event of Humanoid. I've also included a safeguards against stuff loading slowly the first time the Character spawns in:

local Player = Game.Players.LocalPlayer
if not Player.Character then Player.CharacterAdded:wait() end

Player.Character.Humanoid.HealthChanged:connect(function(hp)
	script.Parent.Text = hp
end

1 comment

2

I would also make sure you round up/down the health value to make it an integer, so that when your health is regenerating, it doesn't show it as a long decimal number.

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