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

Make HealthChanged register only once per death?

Asked by 5 years ago

I'm trying to make it so you lose 1 life per death. Using HealthChanged registers several times per death for some reason. I tried using a wait(5) method to add a cooldown, but it's still not working.

local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
_G.Lives = 3

function lives()
    if char.Humanoid.Health == 0 then
    _G.Lives = _G.Lives - 1
    print(_G.Lives)
    wait(5)
    end
end

char.Humanoid.HealthChanged:Connect(lives)

The output of this is the following:

2 1 0 -1 -2

Any advice on how to get this to only happen once per death?

I'm also making a GUI that displays your lives. The GUI seems convinced that _G.Lives is a nil value. I thought putting the _G. in front of the variable would allow my GUI script to see it's value, but apparently not. If someone could explain why my idea of putting _G. in front of the variable isn't fixing the issue in the following code, it would be appreciated.

-- Create ScreenGui
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = script.Parent
local lives = _G.Lives

-- Create TextButton
local textBox = Instance.new("TextBox")

if lives == 2 then
    textBox.Text = "Lives:2"
end

1 answer

Log in to vote
0
Answered by 5 years ago

Hi Garry,

I think you should make use of the Humanoid's .Died event. You can find more information on it here.

Hope I helped and have a wonderful day/night.

Thanks,

Best regards,

~~ KingLoneCat

0
That was exactly what I needed, thank you! Any suggestions on the GUI part? It still says it's nil, but I thought my _G. would allow the variable to be accessed in a separate script? GarryGecko99 30 — 5y
0
One tip: Refrain from using Global Variables, they increase latency. Also, try using the value's .Changed event. Look it up on the wiki. KingLoneCat 2642 — 5y
Ad

Answer this question