Hi, Im made a pathfinding ai that would chase player/ally npc and attack them when in range. However, this seems to be lagging the game
How can i optimize it
local db = false
local animTrack = script.Parent.Humanoid:LoadAnimation(script.Parent.AttackAnim)
local unneededStates = {
Enum.HumanoidStateType.Flying,
Enum.HumanoidStateType.Landed,
Enum.HumanoidStateType.Seated,
Enum.HumanoidStateType.Jumping,
Enum.HumanoidStateType.Climbing,
Enum.HumanoidStateType.Swimming,
Enum.HumanoidStateType.Freefall,
Enum.HumanoidStateType.GettingUp,
Enum.HumanoidStateType.Ragdoll,
Enum.HumanoidStateType.PlatformStanding,
Enum.HumanoidStateType.RunningNoPhysics,
Enum.HumanoidStateType.StrafingNoPhysics,
}
for i , state in pairs(unneededStates) do
script.Parent.Humanoid:SetStateEnabled(state ,false)
end
while true do
local shortestDis = math.huge
local shortestHrp = false
local lists = {}
for i , plr in pairs(game.Players:GetPlayers()) do
local char = plr.Character
local chrp = char:FindFirstChild("HumanoidRootPart")
if chrp then
local hrp = script.Parent.HumanoidRootPart
local dis = (hrp.Position - chrp.Position).Magnitude
if dis < shortestDis then
shortestDis = dis
shortestHrp = chrp
end
end
end
for i , allies in pairs(game.Workspace.Allies:GetChildren()) do
if allies:IsA("Model") and allies:FindFirstChild("Humanoid") then
local char = allies
local chrp = char:FindFirstChild("HumanoidRootPart")
if chrp then
local hrp = script.Parent.HumanoidRootPart
local dis = (hrp.Position - chrp.Position).Magnitude
if dis <= shortestDis + script.Parent.Priority.Value then
shortestDis = dis
shortestHrp = chrp
end
end
end
end
if shortestHrp then
script.Parent.Humanoid:MoveTo(shortestHrp.Position)
end
if (script.Parent.PrimaryPart.Position - Vector3.new(shortestHrp.Position.X , script.Parent.PrimaryPart.Position.Y , shortestHrp.Position.Z)).Magnitude < script.Parent.AttackRange.Value then
if not db then
script.Parent.HumanoidRootPart.punch3:Play()
db = true
local speed = script.Parent.Humanoid.WalkSpeed
script.Parent.Humanoid.WalkSpeed = 0
animTrack:Play()
wait(animTrack.Length/2)
local hrp = script.Parent.HumanoidRootPart
local dis = (hrp.Position - shortestHrp.Position).Magnitude
if (script.Parent.PrimaryPart.Position - Vector3.new(shortestHrp.Position.X , script.Parent.PrimaryPart.Position.Y , shortestHrp.Position.Z)).Magnitude < script.Parent.AttackRange.Value + 5 then
shortestHrp.Parent.Humanoid:TakeDamage(script.Parent.AttackRange.Value)
else
print(dis)
end
wait(animTrack.Length/2)
animTrack:Stop()
script.Parent.Humanoid.WalkSpeed = speed
delay(2 , function()
db = false
end)
end
end
if script.Parent.Humanoid.Health <= 0 then
local explosion = Instance.new("Explosion" , game.Workspace)
explosion.BlastPressure = 0
explosion.DestroyJointRadiusPercent = 0
explosion.ExplosionType = Enum.ExplosionType.NoCraters
explosion.Position = script.Parent["gmod head"].Position
script.Parent.HumanoidRootPart.Death:Play()
script.Parent["gmod head"]:Destroy()
script.Parent.Neck:Destroy()
wait(2)
script.Parent:Destroy()
end
wait(.1)
end