Shield Damage Heal player?

I’m trying to make a shield system that take damage before player if the incoming damage is a “Normal type”
I don’t know what is going on here but for some reason player got healed when their shield taking damage??
I want the player health to stay unchange if their shield still active

Script
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Humanoid = game.Players.LocalPlayer.Character.Humanoid
local rootpart = character:WaitForChild("HumanoidRootPart")
local Event = game:GetService("ReplicatedStorage"):FindFirstChild("Event"):FindFirstChild("Hit")
local Ragdoll = game:GetService("ReplicatedStorage"):FindFirstChild("Event"):FindFirstChild("RagdollSignal")

-- 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

-- Value Set up
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, DamageType, knockback, part) -- calculate the damage
	if DamageType == "Normal Damage" then
		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
			local Anim = Instance.new("Animation")
			Anim.AnimationId = "rbxassetid://101918816996013"
			local track = Humanoid:LoadAnimation(Anim)
			track.Priority = Enum.AnimationPriority.Action
			track:Play()
			Shield.Value = Shield.Value - damage
			Humanoid.Health = oldHealth
		end
		oldHealth = Humanoid.Health
	elseif DamageType == "True Damage" then
		Humanoid.Health = Humanoid.Health - damage
		Ragdoll:FireServer(knockback, part)
	elseif DamageType == "Heal" then
		Humanoid.Health = Humanoid.Health + damage
	end
end)

while true do -- regen Shield if not broken
	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
		elseif Shield.Value > MaxShield.Value then
			Shield.Value = MaxShield.Value
		end
	end
end

Because you’ve defined local oldHealth = Humanoid.Health just before the OnClientEvent connection, so oldHealth is equal to your max health. Then, in this block you set Humanoid.Health = oldHealth which would be equal to the max health at that point.

1 Like

Yes I just realize that thank you for pointing out

1 Like

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