Hi there!
I’m currently in the process of making a new shield system for my game, and for the most part it works fine. The shield keeps track of its own “HP” and when the player is damaged, subtracts that damage from the shield and heals the player accordingly.
However, the problem comes when the player is at low health, as because the script detects damage to the shield by subtracting the current health from the last detected health of the player, when the player’s health reaches something like 5 and is damaged by an attack that deals 500 or so damage, the shield would only receive 5 points of damage.
I’m stuck on how to fix this at the moment, I’d appreciate some help. Here is the script I’m using.
local Tool = script.Parent
local CanEquip = true -- Control the equipping cooldown
local debounce = false
local originalToolName = Tool.Name
local character = Tool.Parent.Parent
local model = game.Workspace:FindFirstChild(character.Name)
local humanoid = model.Humanoid
local ShieldHealth = humanoid.MaxHealth - 1
local CurrentShieldHealth = ShieldHealth
local lastHealth = humanoid.Health
function OnEquipped(mouse)
if CurrentShieldHealth > 0 then
--these just disable the visual effects for damage
humanoid.Parent:FindFirstChild("Z1").Middle.sparks.Enabled = false
humanoid.Parent:FindFirstChild("Z1").Middle.Sound.Enabled = false
humanoid.Parent:FindFirstChild("Z1").Middle.spark1.Enabled = false
humanoid.Parent:FindFirstChild("Z1").Middle.spark2.Enabled = false
humanoid.Parent:FindFirstChild("Z1").Middle.spark3.Enabled = false
humanoid.Parent:FindFirstChild("Z1").Middle.spark4.Enabled = false
humanoid.HealthChanged:Connect(function()
local currentHealth = humanoid.Health
local damage = lastHealth - currentHealth
if damage > 0 then
if CurrentShieldHealth > damage then
CurrentShieldHealth = CurrentShieldHealth - damage
humanoid.Health = currentHealth + damage
if CurrentShieldHealth <= 350 and CurrentShieldHealth > 200 then
script.Parent.Shield.FrontDecal1.Transparency = 0.4
script.Parent.Shield.BackDecal1.Transparency = 0.4
Tool.Shield.Crack:Play()
elseif CurrentShieldHealth <= 200 and CurrentShieldHealth > 100 then
script.Parent.Shield.FrontDecal2.Transparency = 0.4
script.Parent.Shield.BackDecal2.Transparency = 0.4
script.Parent.Shield.FrontDecal1.Transparency = 0.4
script.Parent.Shield.BackDecal1.Transparency = 0.4
Tool.Shield.Crack:Play()
elseif CurrentShieldHealth <= 100 then
script.Parent.Shield.FrontDecal2.Transparency = 0.4
script.Parent.Shield.BackDecal2.Transparency = 0.4
script.Parent.Shield.FrontDecal1.Transparency = 0.4
script.Parent.Shield.BackDecal1.Transparency = 0.4
script.Parent.Shield.FrontDecal3.Transparency = 0.4
script.Parent.Shield.BackDecal3.Transparency = 0.4
Tool.Shield.Crack:Play()
end
else
if CurrentShieldHealth < damage or damage > lastHealth then
if debounce == false then
debounce = true
humanoid.Health = currentHealth + CurrentShieldHealth
CurrentShieldHealth = 0
print("Shield destroyed")
if CurrentShieldHealth == 0 then
Tool.Shield.ShatterSound:Play()
Tool.Shield.FrontDecal1.Transparency = 1
Tool.Shield.FrontDecal2.Transparency = 1
Tool.Shield.FrontDecal3.Transparency = 1
Tool.Shield.BackDecal1.Transparency = 1
Tool.Shield.BackDecal2.Transparency = 1
Tool.Shield.BackDecal3.Transparency = 1
Tool.Shield.CanCollide = false
Tool.Shield.ParticleEmitter.Enabled = false
Tool.Particle.Thingy.Enabled = false
Tool.Shield.Transparency = 1
Tool.Ring.Transparency = 1
Tool.ZTimer.Transparency = 1
humanoid.Parent:FindFirstChild("Z1").Middle.sparks.Enabled = true
humanoid.Parent:FindFirstChild("Z1").Middle.Sound.Enabled = true
print("Sparks enabled!")
Tool.Shield.Shatter.Enabled = true
wait(0.2)
Tool.Shield.Shatter.Enabled = false
wait(1)
Tool.Name = "Barrier [COOLDOWN]"
-- Unequip the shield and start cooldown
debounce = false
humanoid:UnequipTools()
wait(45)
CurrentShieldHealth = ShieldHealth
Tool.Shield.CanCollide = true
Tool.Shield.ParticleEmitter.Enabled = true
Tool.Particle.Thingy.Enabled = true
Tool.Shield.Transparency = 0
Tool.Ring.Transparency = 0
Tool.ZTimer.Transparency = 0
Tool.Name = originalToolName
end
end
end
end
end
end)
else if CurrentShieldHealth <= 0 then
task.wait()
print("Route two!")
humanoid:UnequipTools()
end
end
end
Tool.Unequipped:Connect(function()
local character = Tool.Parent.Parent
local model = game.Workspace:FindFirstChild(character.Name)
local humanoid = model.Humanoid
local sparks = model.Z1.Middle.sparks
local sound = model.Z1.Middle.Sound
sound.Enabled = true
sparks.Enabled = true
humanoid.HealthChanged:Connect(function()
if humanoid.Health < lastHealth then
lastHealth = humanoid.Health
end
end)
end)
Tool.Equipped:connect(OnEquipped)