Hello developers.
I’ve have an issue with an unit I have within my 2D, Tower Defense game. Basically, the unit has a rage state which reaches whenever the unit reaches 40% of their health left which they’ll go into a rage state where they get increased damage, higher attack rate, faster speed, and immunity to ALL damage with the cost of them rapidly damaging themselves.
I’ve found an issue however, as seen in the video below, whenever the enemy variant of the unit triggers the friendly unit’s rage state, it basically skips the next enemy variant of the unit.
This is the AI (Including rage function):
--//AI
local DB = false
local Enrage = false
--//Enrage
local EnrageSelfDamage = coroutine.create(function()
while task.wait() and Humanoid.Health >= 1 do
Humanoid:TakeDamage(3.6)
end
end)
Humanoid.HealthChanged:Connect(function(Health)
if (Health / Humanoid.MaxHealth * 100) <= 40 and not Enrage then
Enrage = true
print("I'M ANGRY!!!")
OriginSpeed = 24
Humanoid.WalkSpeed = OriginSpeed
AttackRate = 1
MissileDamage = 250
local ForceField = Instance.new("ForceField",FriendCommander)
ForceField.Visible = false
coroutine.resume(EnrageSelfDamage)
end
end)
while task.wait() and Humanoid.Health >= 1 do
for _, Enemy in pairs(workspace.ExistingEntities:GetChildren()) do
if Enemy:IsA("Model") and Enemy:HasTag("EnemyTag") then
local EnemyHum = Enemy:FindFirstChildWhichIsA("Humanoid")
local Magnitude = (FriendCommander.PrimaryPart.Position - Enemy.PrimaryPart.Position).Magnitude
if Magnitude <= 30 and not DB and EnemyHum.Health >= 1 then
DB = true
Humanoid.WalkSpeed = 0
task.wait(0.2)
FriendCommander.Missile.FireSound:Play()
local FireAnim = Animator:LoadAnimation(script.Fire)
FireAnim:Play()
local Explosion = Instance.new("Explosion",workspace.Debris)
Explosion.Position = Enemy.PrimaryPart.Position
Explosion.BlastPressure = 0
Explosion.DestroyJointRadiusPercent = 0
local hits = {}
Explosion.Hit:Connect(function(hit)
local model = hit:FindFirstAncestorWhichIsA("Model")
local hum = model and model:FindFirstChildWhichIsA("Humanoid")
if hum and not hits[model] then
hits[model] = true
hum:TakeDamage(MissileDamage)
task.wait(3)
hits[model] = false
end
end)
task.wait(AttackRate)
DB = false
Humanoid.WalkSpeed = OriginSpeed
end
end
end
end