I made an AI but it start normaly but after a while it dont attack and dont stop in it range:local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PathFindingService = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")
local Modules = ReplicatedStorage.Modules
local Events = ReplicatedStorage.Events
local AnimationEvent = Events.AnimationEvent
local AttackModules = Modules.Attacks
local troop = {}
local path = nil
local waypoints = nil
local NPC = nil
local Configuration = nil
local isWalking = false
local lastPos = nil
local function Attack(target)
local moduleObj = AttackModules:FindFirstChild(NPC.Name)
if moduleObj then
local module = require(moduleObj)
module.Attack(target, NPC)
return
else
warn("No attack module for " .. NPC.Name)
return
end
end
local function CreatePath(target)
local PlayerPath = PathFindingService:CreatePath({
AgentCanJump = true,
})
local targetHRP = target and target:FindFirstChild("HumanoidRootPart")
if not targetHRP then return nil end
local success, err = pcall(function()
PlayerPath:ComputeAsync(NPC.HumanoidRootPart.Position, targetHRP.Position)
end)
if not success or PlayerPath.Status ~= Enum.PathStatus.Success then
return nil
end
return PlayerPath
end
local function GetClosestEnemy()
local closest = nil
local shortestDist = math.huge
for _, enemy in ipairs(workspace.Enemys:GetChildren()) do
if enemy and enemy:FindFirstChild("HumanoidRootPart") then
local dist = (enemy.HumanoidRootPart.Position - NPC.HumanoidRootPart.Position).Magnitude
if dist < shortestDist and enemy.Humanoid.Health > 0 then
shortestDist = dist
closest = enemy
end
end
end
return closest
end