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

How do I properly scale damage for an armor system?

What I have tried to do is create an armor system that properly absorbs and scales down damage taken instead of just increasing the player's MaxHealth. I have been able to do this but I ran across a problem where the script stops scaling properly and the player takes more damage as their health lowers, I think I didn't go the right way about doing this and I'm stumped.

Code (This is in a script inside of the player and it also accesses a armor durability value inside of the player and I'm sorry if it's spaghetti code I haven't scripted in a while):

function absorbDamage()
	if script.Parent.Durability.Value <= 0 then return end
	local newHealth = script.Parent.Humanoid.Health
	
	if newHealth < oldHealth ~= true then return end
	
	if newHealth < oldHealth then
		if debounce == false then
			debounce = true
			
			local damage = script.Parent.Humanoid.MaxHealth - script.Parent.Humanoid.Health
			script.Parent.Humanoid.Health = script.Parent.Humanoid.MaxHealth
						
			wait(0.1)
			local scaleDamage = damage*0.60
			print(scaleDamage)
			script.Parent.Humanoid:TakeDamage(scaleDamage)
		
			script.Parent.Durability.Value = script.Parent.Durability.Value - 5

			wait(1.5)
			debounce = false
		end	
	end
end

script.Parent.Humanoid:GetPropertyChangedSignal("Health"):Connect(absorbDamage)

1 answer

✓ Accepted answer

Think about it. The damage variable in your script is the difference between the current health and the humanoid's maximum health.

If the humanoid's health decreased from 64 to 13, the difference would be 87 points (not 51, since you're subtracting it from the humanoid's maximum health), which scales to 52.2.

You should instead find the difference between the humanoid's current health and the humanoid's health from before it took damage. So the scaled damage from 64 to 13 would be 30.6 (51 × 0.6).

5 comments

0

I rewrote the script to find the damage by subtracting the old health from the new health instead of the maximum health, and redefined the old health at the end of the script to be the player's health after the damage was absorbed. This fixed the issue of the scaled damage increasing over time but now the player just won't be damaged at all, or will be healed instead of damaged

0

Did you keep line 12 as-is? Where you're setting the player's health to MaxHealth?

0

Yes

0

It should be oldHealth, not MaxHealth. If my answer worked, do accept it. As this is beyond the scope of the asked question.

0

I apologize, I still barely use this site, but thank you for the help and I will accept

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