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

Health GUI glitching when character dies, but no errors are outputted?

Hello, I have recently been trying to make a health display system for an RPG game, and it does work for a short time, until the character dies. However to make things worse, there is no error in the output, script analysis or the dev console when runned in servers. Now, I have narrowed it down to two scripts which could possibly causing this, but I am not sure which one and at which point it is causing the problem so that when the character dies, the characters HP displays as "0/(HP Number)", and when tested, the players HP is automatically set back to 100, despite the max health still displaying as whatever the players max health has reached (e.g. 865 for max), help would greatly be appreciated.

Script that displays the HP:

local Player = game.Players.LocalPlayer
local Character
repeat
Character = Player.Character
wait(0.5)
until Character
local Humanoid
repeat
Humanoid = Character.Humanoid
wait(0.5)
until Humanoid

while true do
	wait(0.5)
    repeat wait (0.1) until Humanoid ~= nil
	script.Parent.Text = "HP: "..Humanoid.Health.."/"..Humanoid.MaxHealth
end

Script that changes the HP:

local Player = game.Players.LocalPlayer
local Character
repeat
Character = Player.Character
wait(0.5)
until Character
local Humanoid = Character:WaitForChild('Humanoid')
local Stats
repeat
Stats = Player.Stats
wait(0.5)
until Stats
local Level = Stats:WaitForChild('Level')
local Defence = Stats:WaitForChild('Defence')

while true do
	wait(0.5)
	script.Parent.Text = Level.Value
    repeat wait (0.1) until Character ~= nil
	Humanoid.MaxHealth = (100+(Defence.Value+(Level.Value*25)))
end

2 comments

0

What’s with line 20? You’ve gone crazy with the brackets. They are plain redundant and only unnecessarily extend your lines of code.

0

@incapaz please keep it relevant, that was when I attempted to fix it, thinking it might be adding up wrong, it makes no difference.

1 answer

I don't like to use "while true do", i preffer just use the Changed event for avoid crashes if something gone wrong. Your script is probably having issues while try to divide 0 by something, try this way:

repeat wait (0.1) until Humanoid ~= nil and Humanoid.Health > 0

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