Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to create resistance while having armor on?

A couple of friends and I are making a game, and the coder is having problems with something. He is trying to create if you have armor on, you are resistance to damage. Here is the script he sent me:

local tester = script.Parent

tester.Humanoid.HealthChanged:Connect(function(hpleft)
   local hplost = (tester.Humanoid.MaxHealth - hpleft)
    tester.Humanoid.Health = tester.Humanoid.Health + (hplost * 0.5)
end)

(50% resistance)

He said the error was:

First, the script listens for humanoid.HealthChanged, then it will set the current health + half the amount of hp lost but that would fire humanoid.HealthChanged.

1 answer

✓ Accepted answer

Not sure if this will work but use a debounce to make sure it doesn't run twice at once.

local tester = script.Parent
local debounce = false

tester.Humanoid.HealthChanged:Connect(function(hpleft)
	if debounce = true then return end -- stops the function from running twice
	debounce = true
	local hplost = (tester.Humanoid.MaxHealth - hpleft)
	tester.Humanoid.Health = tester.Humanoid.Health + (hplost * 0.5)
	wait() -- i think this is an optional wait
	debounce = false
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.