So, this script is basically a healthbar that shows different frame depending on player’s health level. I made it using answers from my previous question. The thing is, it works perfectly, but when player dies it stops working(The frames are not even showing)
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local human = char:WaitForChild("Humanoid")
local hud = player.PlayerGui.Hud.ImageLabel
local fine = hud.fine
local fine2 = hud.fine2
local caution = hud.caution
local danger = hud.danger
for _,player in pairs(game.Players:GetPlayers()) do
human.HealthChanged:Connect(function(Dmg)
local hp = human.Health
fine.Visible = hp >= 80
fine2.Visible = hp >= 60 and hp < 80
caution.Visible = hp >= 30 and hp < 60
danger.Visible = hp < 30
end)
end
And there’s some of the methods that don’t work:
ResetOnRespawn = true just stops the script on “danger” frame.
This script:
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
local hud = player.PlayerGui.Hud.ImageLabel
local fine = hud.fine
local fine2 = hud.fine2
local caution = hud.caution
local danger = hud.danger
humanoid.HealthChanged:Connect(function(health)
fine.Visible = health >= 80
fine2.Visible = health >= 60 and health < 80
caution.Visible = health >= 30 and health < 60
danger.Visible = health < 30
end)
end)
end)
Also don’t work, the frames are not even showing too, I also tried to add WaitForChild’s as recommended, but it didn’t change anything, the script’s still aint working
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
local hud = player.PlayerGui.Hud:WaitForChild("ImageLabel")
local fine = hud:WaitForChild("fine")
local fine2 = hud:WaitForChild("fine2")
local caution = hud:WaitForChild("caution")
local danger = hud:WaitForChild("danger")
humanoid.HealthChanged:Connect(function(health)
fine.Visible = health >= 80
fine2.Visible = health >= 60 and health < 80
caution.Visible = health >= 30 and health < 60
danger.Visible = health < 30
end)
end)
end)
whenever you die, the character gets deleted.
But the value char will still be set to the old (now non existant) character.
To fix this, you should take an event from when a player has respawned, and whenever the event gets fired, it sets the Char value and all values associated with it to the new Character.
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
local hud = player.PlayerGui.Hud.ImageLabel
local fine = hud.fine
local fine2 = hud.fine2
local caution = hud.caution
local danger = hud.danger
humanoid.HealthChanged:Connect(function(health)
fine.Visible = health >= 80
fine2.Visible = health >= 60 and health < 80
caution.Visible = health >= 30 and health < 60
danger.Visible = health < 30
end)
humanoid:TakeDamage(1)
end)
end)
Here, the humanoid takes 1 damage which should cause the “HealthChanged” event to fire which will result in the UI resetting itself.
local function Update(Health, fine,fine2,caution,danger)
fine.Visible = health >= 80
fine2.Visible = health >= 60 and health < 80
caution.Visible = health >= 30 and health < 60
danger.Visible = health < 30
end
local humanoid = character:WaitForChild("Humanoid")
local hud = player.PlayerGui.Hud:WaitForChild("ImageLabel")
local fine = hud:WaitForChild("fine")
local fine2 = hud:WaitForChild("fine2")
local caution = hud:WaitForChild("caution")
local danger = hud:WaitForChild("danger")
Update(Humanoid.Health,fine,fine2,caution,danger)
humanoid.HealthChanged:Connect(function(health)
Update(health,fine,fine2,caution,danger)
end)
end)
local function Update(health, fine,fine2,caution,danger)
fine.Visible = health >= 80
fine2.Visible = health >= 60 and health < 80
caution.Visible = health >= 30 and health < 60
danger.Visible = health < 30
end
local humanoid = character:WaitForChild("Humanoid")
local hud = player.PlayerGui.Hud:WaitForChild("ImageLabel")
local fine = hud:WaitForChild("fine")
local fine2 = hud:WaitForChild("fine2")
local caution = hud:WaitForChild("caution")
local danger = hud:WaitForChild("danger")
Update(humanoid.Health,fine,fine2,caution,danger)
humanoid.HealthChanged:Connect(function(health)
Update(health,fine,fine2,caution,danger)
end)
end)