Excessive Damage Calculation error

I’m making a shield system and trying to calculate remaining damage that the shield can’t block then apply that to player
the blue bar is shield
the red bar is health
Max shield is 100
the attack deal 125 damage
so in theory player should only take 25 Excessive damage right?
well not in my case

from the video you can see that my shield does take damage and Excessive damage is calculated correctly but for some reason you just straight up die

Shield Script
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Humanoid = game.Players.LocalPlayer.Character.Humanoid
local Event = game:GetService("ReplicatedStorage"):FindFirstChild("Event"):FindFirstChild("Hit")

-- Configuration
local Max_Shield = 100
local Stater_Shield = 100
local Shield_Regen_Amount = 3/100
local Shield_Regen_Rate = 0.250
local Shield_Break = false
local Break_Recover_Cooldown = 3

local MaxShield = Instance.new("NumberValue")
MaxShield.Value = Max_Shield
MaxShield.Name = "MaxShield"
MaxShield.Parent = Humanoid

local Shield = Instance.new("NumberValue")
Shield.Value = Stater_Shield
Shield.Name = "Shield"
Shield.Parent = Humanoid

local oldHealth = Humanoid.Health

Event.OnClientEvent:Connect(function(damage)
	Humanoid:TakeDamage(damage)
end)

Humanoid:GetPropertyChangedSignal("Health"):connect(function()
	local Damage = oldHealth - Humanoid.Health
	print("Damage Taken: ", Damage)
	local ExcessiveDamage
	if Damage >= Shield.Value then
		ExcessiveDamage = Damage - Shield.Value
		print("ExcessiveDamage: ", ExcessiveDamage)
		Shield.Value = 0
		Shield_Break = true
		Humanoid.Health = Humanoid.Health - ExcessiveDamage
	elseif Damage < Shield.Value then
		Shield.Value = Shield.Value - Damage
		Humanoid.Health = oldHealth
	end
	oldHealth = Humanoid.Health
end)

while true do
	if Shield_Break == true then
		Shield_Regen_Amount = 0
		wait(Break_Recover_Cooldown)
		Shield_Regen_Amount = 3/100
		Shield_Break = false
	elseif Shield_Break == false then
		wait(Shield_Regen_Rate)
		if Shield.Value < MaxShield.Value then
			Shield.Value = Shield.Value + Shield_Regen_Amount * MaxShield.Value
		end
	end
end

Humanoid dead is calculated before adding life, so when you try to add life the humanoid is already dead.

You should move all the calculation to OnClientEvent, if there is another way for the humanoid to take damage (idk, fall damage) you could use a BindableEvent, or make an OOP module and control its damage with a function.

would you mine if I ask what is OOP? I saw this so many time while doing the researching, I’m still new to lua coding in general

OOP is object-oriented programming, basically it is a table with methods and everything (obviously) oriented to manipulate an Instance, you can have a table to control its inventory, states, appearance, etc. It is simply data saved for that specific Instance.

1 Like

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