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

Need help Completing My System? NEW

Ignore the title...

So uhh... Yeah... Im trying to make it when the character has taken damage... a gui shows up... Okay here's what i put

local hum = script.Parent.Humanoid
local RG = game.Players.LocalPlayer.PlayerGui.DamageGui

hum.HealthChanged:Connect(function()
	RG.Enabled = true
	wait(2)
	RG.Enabled = false
end)

This is in Starter Character... Instead of when Damaged... It does it when it is healing and stuff to... How could i make this where the GUI only pops up on Damage.

2 answers

✓ Accepted answer

The solution is to check if the health is going down, here is how.

local PreviousHealth = 101
local hum = script.Parent.Humanoid
local RG = game.Players.LocalPlayer.PlayerGui.DamageGui

hum.HealthChanged:Connect(function() -- when changed..
	if hum.Health < PreviousHealth then -- is the health less than the previous health?
	  RG.Enabled = true -- open ui
 	  wait(2)
  	  RG.Enabled = false -- close ui
	end
	PreviousHealth = hum.Health -- set the health in case it goes up
end)

If this helps, please click accept, if not message me in the comments. Good luck!

You can do this by keeping a variable which holds the previous health, then compare each time when it gets called and check if the current health is lower than the previous health

Example

local hum = script.Parent.Humanoid
local RG = game.Players.LocalPlayer.PlayerGui.DamageGui
local prevHealth = hum.Health

hum.HealthChanged:Connect(function(health)
	if prevHealth > health then -- check if prev health is lower than current health
		RG.Enabled = true
    		wait(2)
		RG.Enabled = false
	end
	prevHealth = health -- set the next previous health
end)

7 comments

0

n000000000 im too SLOW

0

how did you get upvoted and i dont ??

0

bro idk

0

There... THX ALL OF YOU GUYS!!!!!!

0

Alright now imma go to sleep...

0

Ima upvote this because you did exactly what I did.

0

Great minds think alike ;)

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