Prevent players from dying at or below 0 hp

Hi! I’m trying to make a totem of undying-like tool that prevents the player from dying once their health reaches 0 or below. I don’t really know how to accomplish this, so I would appreciate any help you guys could give me. My first attempt was this:

repeat 
	wait(.001)
until character.Humanoid.Health <= 0

character.Humanoid.Health = 100

Again, I just wanna find a way to prevent a player from dying once their health drops below zero ;-;

You could try using the Humanoid’s SetStateEnabled function to set the “Dead” state to false so they never enter it when they have the totem. And set it back to true again when they don’t have the totem. When a Humanoid’s health hits 0 they enter the Dead state, so disabling that state should keep them alive I think. In bed about to pass out or I’d try it and check.

through some quick searching, there’s a topic with a similar question months ago with a answer: How to disable dying at 0 health - #2 by Sepruko

it’s kinda identical to what @drumzart suggested, it just involves turning off the dead state on both the server & client with Humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false).

i did some testing to confirm it as well:
-server- (in ServerScriptService)

--services--
local PlayerService = game:GetService("Players")

--events--
PlayerService.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:FindFirstChildOfClass("Humanoid")
		if Humanoid then
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false)
		end
	end)
end)

-client- (in StarterCharacterScripts)

local Hum = script.Parent:FindFirstChildOfClass("Humanoid")
Hum:SetStateEnabled(Enum.HumanoidStateType.Dead, false)

a silly mistake i did while testing was forgetting to enable the local script, which would result in the player’s character dying normally, but never respawning. after enabling it, it worked for me.

4 Likes

Pls don’t use the wait(), it’s deprecated and bad, use task.wait() instead.

It is good practice to use task.wait(), but there aren’t really problems with wait() except for a bit of throttling and waiting for incorrect time (by less than a second)

oh ye I quickly wrote that loop up, I always try to use task.wait()

true, but task.wait is generally better to use

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.