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

Why won't this Hurt Gui Popup?

I plan to make a Gui that Appears when a player's health is below 20, if it is over 20 the Gui will not popup

FYI: NO ERROR POPPED UP

Here is my Code:

local s=Instance.new("Sound")--created a Sound

if game.Players.LocalPlayer.Character.Humanoid.Health<=20 then
	s.SoundId="http://www.roblox.com/asset/?id=149532107"
	s.Parent=workspace
	s.Looped=true
	script.Parent.Visible=true--the script.Parent is the Frame
else
	if game.Players.LocalPlayer.Character.Humanoid.Health>=20 then
		script.Parent.Visible=false
		s.Parent=workspace
		s:stop()
	end
end

while true do
	script.Parent.BackgroundTransparency=0.2
	wait(0.1)
	script.Parent.BackgroundTransparency=0.1
	wait(0.1)
	script.Parent.BackgroundTransparency=0
	wait(0.1)
	script.Parent.BackgroundTransparency=0.1
	wait(0.1)
	script.Parent.BackgroundTransparency=0.2
	wait(0.1)
	script.Parent.BackgroundTransparency=0.3
	wait(0.1)
end

*Please Reply back I would appreciate it! *

2 answers

✓ Accepted answer

Your problem is that that if chunk is only ever called once: when the script is first ran. You have to connect it to an Event or put it in a loop. Luckily, the HealthChanged Event of Humanoid works for our needs!

game.Players.LocalPlayer.Character.Humanoid.HealthChanged:connect(function(health)
	if health <= 20 then
	    s.SoundId = "http://www.roblox.com/asset/?id=149532107"
	    s.Parent = workspace
	    s.Looped = true
	    script.Parent.Visible = true
	else --if health > 20 then --The if statement here is actually unnecessary, and `elseif` works in Lua.
	    script.Parent.Visible = false
	    s.Parent = workspace
	    s:stop()
	end
end)

4 comments

0

Why are you using "Changed" instead of "HealthChange"?

0

and you forgot an "end)"

0

works thx

0

I was going to use Changed, but read up on HealthChanged and forgot to change my code to reflect that. As far as I am aware, there is no missing end, just a missing closing parenthesis.

Are you using a LocalScript? Where is this script located at? Is your hierarchy correct? Assuming everything is good, try to use elseif instead. Line 8 would look like this:

elseif game.Players.LocalPlayer.Character.Humanoid.Health>=20 then

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