How do I make a tool that cheats death for the player?

Hello everyone!

I’m back (again) with another question!

I’m trying to make a tool that functions similar to the Totem of Undying from Minecraft; when the player takes fatal damage, the tool activates, preventing the player from dying but being consumed in the process.

I currently have this script set up:

local tool = script.Parent

tool.Equipped:Connect(function()
	local Character = tool.Parent
	local Humanoid = Character.Humanoid
	if Humanoid.Health == 0 then
		Humanoid.Health = 10
		wait()
		tool:Destroy()
	end
	wait()
end)

It’s in a serverscript as a child of the tool, and the tool is currently in StarterPack but I hope to soon move it to replicatedstorage for use ingame.

Currently, the script doesn’t throw any errors, but the player still dies. Is there any way to get this to work? Thanks in advance! :slight_smile:
~innit_2winnit

Use that when player equips the item
Humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false)
Normal event of player humanoid health changed should still be called with value of 0, so after that event is called, set that state back to true and destroy the item.

EDIT: I think it is important to also mention that your code would never work, because Equipped event is called when player equips the event, and so it’s impossible for the player to have 0 health.
Look at the code from logical view and try to step your code in your head. In Roblox you use events for different things, including player health change Humanoid.HealthChanged:Connect(function(hp) ... end)

1 Like

I’m sorry, I don’t really get what you mean.
Best I could come up with is this:

local tool = script.Parent

tool.Equipped:Connect(function()
	local Character = tool.Parent
	local Humanoid = Character.Humanoid
	Humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false)
	Humanoid.HealthChanged:Connect(function(hp)  
		hp = 10
		wait()
		tool:Destroy()
	end)
end)

But it doesn’t save the player from death.

Then use print and go debug if your code works properly…
People should learn how to debug their code.

I was able to solve it by asking AI to help me debug it.