So I’m trying to make a dummy that resets its health to 100 after 5 seconds when its health is 0.
And this is the code:
local Dummy = script.Parent
local Humanoid = Dummy:WaitForChild("Humanoid")
while wait() do
if Humanoid.Health == 0 then
wait(5)
Humanoid.Health = 100
end
end
But for some reason it doesn’t work, After I made the dummy’s health to 0, I went to the server and tried to change the Health value manually but it’s just stuck on 0. The second I tried to change the health it’s back to 0.
local Dummy = script.Parent
local Humanoid = Dummy:WaitForChild("Humanoid")
local function resetHealth()
if Humanoid.Health == 0 then
wait(5)
-- Ensure the Humanoid is not in a "dead" state
Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
Humanoid.Health = 100
end
end
-- Monitor the Humanoid's health
Humanoid.HealthChanged:Connect(function(health)
if health == 0 then
resetHealth()
end
end)
local Dummy = script.Parent
local function resetHealth(humanoid)
if humanoid.Health == 0 then
wait(5)
--Ensure the Humanoid is not in a "dead" state
humanoid:ChangeState(Enum.HumanoidStateType.Physics)
humanoid.Health = 100
end
end
-- Watch for new Humanoids being added to the workspace
game.Workspace.DescendantAdded:Connect(function(descendant)
if descendant:IsA("Humanoid") then
local humanoid = descendant
humanoid.HealthChanged:Connect(function(health)
if health == 0 then
resetHealth(humanoid)
end
end)
end
end)
By default when your humanoid’s health is at 0, they enter the dead state where your humanoid cannot gain health more than 0, Because the logic is made to prevent health from healing, you would need to disable that state and make your own dead state when it reaches 0, it’s like you’re giving them a blood and you expect the dead body to revive but remains dead forever.
also there’s no reason to use while loops for this when HealthChanged event exists for the humanoid
local dummy = script.Parent
local humanoid = dummy:WaitForChild("Humanoid")
humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false) --disables the state
function reset_hp_back()
task.wait(2) --set the timer here
humanoid.Health = 100
end
humanoid.HealthChanged:Connect(function(num)
if num <= 0 then
reset_hp_back()
end
end)
a reminder that changing state to either physics or anything that isnt dead will NOT work as you’re only changing it to run that state, humanoid will still die forever if you dont disable that dead state before it enters.
just remember to disable BreakJointsOnDeath if you want it in the shape you’re supposed to be, otherwise it’ll still fall even that disabled like in the video,…,…,.,…,.,.,.