Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why won't this Hurt Gui Popup?

Asked by
woodengop 1134 Moderation Voter
9 years ago

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

Log in to vote
3
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

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)
0
Why are you using "Changed" instead of "HealthChange"? woodengop 1134 — 9y
0
and you forgot an "end)" woodengop 1134 — 9y
0
works thx woodengop 1134 — 9y
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. adark 5487 — 9y
Ad
Log in to vote
-1
Answered by 9 years ago

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

Answer this question