I am currently making an enemy that focuses on gathering resources for it’s team, but won’t hesitate to defend itself if it’s attacked. I have it mostly working, the issues is that it keeps attacking faster and faster. I don’t see any issues that could cause this and neither did the AI assistant. Can anyone help me solve this?
Below is the part of the MainAIScript that dictates whether or not it’s in combat and whether or not is should run or pursue attackers.
–inCombatFunction
local function inCombatFunction()
local totalTimeInCombat = Worker.totalTimeInCombat
for i,v in ipairs(game:GetService(“Players”):GetPlayers())do
repeat
wait()
if (v.Character.PrimaryPart.Position - Worker.PrimaryPart.Position).Magnitude <= DetectThreatRange and target == nil then
target = v
print(v.Name)
Humanoid:MoveTo(target.Character.PrimaryPart.Position - CFrame.new(Worker.PrimaryPart.Position, target.Character.PrimaryPart.Position).LookVector * attackRange)
Worker.Humanoid.WalkSpeed = 15
attackRemote:Fire(v)
print(“Attack requested”)
elseif (v.Character.PrimaryPart.Position - Worker.PrimaryPart.Position).Magnitude <= DetectThreatRange and target ~= nil then
Humanoid:MoveTo(target.Character.PrimaryPart.Position - CFrame.new(Worker.PrimaryPart.Position, target.Character.PrimaryPart.Position).LookVector * attackRange)
Worker.Humanoid.WalkSpeed = 15
attackRemote:Fire(v)
print(“Attack requested”)
elseif (v.Character.PrimaryPart.Position - Worker.PrimaryPart.Position).Magnitude > DetectThreatRange then
Humanoid.WalkSpeed = 25
Humanoid:MoveTo(parentHive.Parent.PrimaryPart.Position)
Humanoid.MoveToFinished:Connect(function()
Humanoid.WalkSpeed = 15
end)
continue
end
totalTimeInCombat.Value += 1
until totalTimeInCombat.Value == inCombatTimer
end
end
Below here is the AIAttackScript that fires if it decides it should pursue its attackers.
local entity = script.Parent.Parent
local minDashRange = 20
local maxDashRange = 50
local meleeAttackRange = 19
local meleeAttackWindUp = 1
local dashDamage = 10
local meleeDamage = 4
local attackRemote = entity.AIMainScript.AttackRemote
attackRemote.Event:Connect(function(v)
if (entity.PrimaryPart.Position - v.Character.PrimaryPart.Position).Magnitude <= meleeAttackRange and entity.canAttack.Value == true then
entity.canAttack.Value = false
print(“Attack unavailable”)
entity.Humanoid.WalkSpeed = 0.1
wait(meleeAttackWindUp)
if v and v.Character and (entity.PrimaryPart.Position - v.Character.PrimaryPart.Position).Magnitude <= meleeAttackRange then
v.Character.Humanoid:TakeDamage(meleeDamage)
end
entity.Humanoid.WalkSpeed = 15
end
repeat
wait(1)
entity.attackCooldown.Value -= 1
until entity.attackCooldown.Value == 0
print(“Attack available”)
entity.canAttack.Value = true
entity.attackCooldown.Value = 600
end)
As a reminder, the issue is that the enemy keeps attacking faster and faster, effectively ignoring the attackCooldown after a time and killing the player near instantaneously.