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