Damage absorption

So I really don’t know how to make a damage absorption script and I can’t explain what the situation is really about but here.

What I am trying to do is to create a damager absorber that absorbs damage depending on where your health wants to be stopped at. For example, a rocket launcher deals 100 damage and if the player’s health is willing to be stopped at 35, instead of 100 damage, it only deals 65 damage.

I can’t explain this well so tell me if you are confused. Otherwise, I don’t know how to create this system.

Do you mean kinda like Minecraft absorption where you gain extra temporary hearts? Or do you mean something else? Either way it can be pretty simple depending on how you have things setup.

If you are able to clarify a little more, that’d be great. I’m pretty confused too

Here are my few suggestions depending on what you want to do;

  • Make a module that handles player damage so you can easily customize it and add these effects.
  • If you just want to give them more health, just increase their health.
  • If you just want to make them invincible for a short period of time, give them an invisible forcefield or set their health and max health very high.

So, what I am trying to say is the rocket launcher deals 100 damage. And the player has 100 hp. If I want to create a system if the player reaches to 35 hp, the leftover damage will be canceled out. So instead of the rocket launcher dealing 100 damage, it only deals 65. Later on, if the player has 35 hp left and someone is trying to shoot the player with the rocket launcher, it deals no damage. I hope this helps because recall that I can’t explain well but I know what I’m trying to do.

My goal is to not increase the max health, but the 1st idea could possibly work.

Yeah, I think you should start handling all of the damage dealing through a module script and just execute the function that deals damage to do that in all your weapons. This will mean you will not have to repeat your if statements about checking the player’s current HP throughout all of your weaponry.

Can you be a bit more specific because you are telling me that All of the weapons should be reduced when a player is at 35 hp right? (I dont understand what I’ve said tbh)

So you only want the rocket launcher to have reduced damage when the player is on low HP?

what i am saying is there should be no damage dealt when the player is at 35 hit points or lower.

Let me give you an example of the module you could create.

local module = {}

function module:DamagePlayer(player, amount)
    local character = player.Character
    if character == nil then return end
    local humanoid = character.Humanoid
    if humanoid == nil then return end
    if humanoid.Health > 35 then
        humanoid:TakeDamage(amount)
    end
end


return module

And you can add more health checks in there to do different things.

Example in a weapon:

local damage = 20
local damageModule = require(damageModuleLocation) -- Replace this with the module location/path.

local function damageTarget(player, damage) -- Call this function whenever the player should take damage.
    damageModule:DamagePlayer(player) -- Calls the function in the module.
end

Use Humanoid.HealthChanged to detect when the Humanoid’s health changes. If the change in health is too much, set the Humanoid’s health to the desired amount.

Example:

local MinHealth = 35

Humanoid.HealthChanged:Connect(function(NewHealth)
	if NewHealth < MinHealth then
		Humanoid.Health = MinHealth
	end
end)

This script will make it impossible for health to drop bellow 35.

Where do I put this script? On a rocket or the studio? I rather put it on the rocket than the studio because I dont want the player to be immortal.

ok idk where to exactly put this script but It looks like it might work

The script I made before would make the player immortal and never be able to take more than 65 damage. If you don’t want the player to be immortal, you can do something like this:

local MaxDamage = 65 --The maximum damage someone can take at a time

local OldHealth = Humanoid.Health
Humanoid.HealthChanged:Connect(function(NewHealth)
	if (OldHealth - NewHealth) > MaxDamage then
		Humanoid.Health = OldHealth - MaxDamage
	end
	OldHealth = NewHealth
end)

This script would be located in the character or in studio, not the weapon.

HealthChanged is a client-sided event, and you can’t update the health on the client because it doesn’t replicate.

I tested it myself, it works on the server. If for some reason it does not work, use GetPropertyChangedSignal or an alternative. The script I created should be put in a server script.

This is untrue. If someone is dealt their maximum health or greater, it will still change their health before they die. If the rocket breaks the welds of the character, though, this script will not work because roblox will set the humanoid health to zero every single frame. Make sure your weapons do not destroy character parts.

Then they changed it recently.
HealthChanged was previously only available on the client-side.

image
This is what I got but I found out that there is something wrong with the Humanoid Part. Sorry for the delay.

You need to define Humanoid. I purposely did not include that part because I did not know where you were putting the script. Something like this should work if the script is put directly under the character (In StarterCharacterScripts):

local Humanoid = script.Parent:WaitForChild("Humanoid")
local MaxDamage = 65 --The maximum damage someone can take at a time

local OldHealth = Humanoid.Health
Humanoid.HealthChanged:Connect(function(NewHealth)
	if (OldHealth - NewHealth) > MaxDamage then
		Humanoid.Health = OldHealth - MaxDamage
	end
	OldHealth = NewHealth
end)