I am trying to make a pathfinding AI that chases the nearest player, however I keep running into an issue with the AI to where overtime it begins to stutter making it slow down as time passes by in the server.
In the video you’ll see that at first it’s fine but overtime it’ll end up slowing down and stuttering:
I’ve tried some already existing solutions from other topics however none have worked so far and I’m not too experience in pathfinding or advanced scripting.
--Services
local Players = game:GetService("Players");
local RunService = game:GetService("RunService");
local PathFindingService = game:GetService("PathfindingService");
--Variables
local Character = script.Parent;
local Humanoid = Character.Monster;
local Config = Character.Configuration;
local atksfx = Character.Torso.attack;
local RootPart = Character.HumanoidRootPart;
local CanJump = true;
local AttackRange = Config.AttackRange.Value;
local AttackDamage = Config.AttackDmg.Value;
local AttackInterval = Config.AttackInterval.Value;
local CharacterSize = Character:GetExtentsSize();
local PathParams = {
AgentRadius = (CharacterSize.X+CharacterSize.Z)/4,
AgentHeight = CharacterSize.Y,
AgentCanJump = CanJump,
};
local CurrentTarget = nil;
--Attack Animations
local Attackanim1 = script:WaitForChild("Attack1")
local Attackanim2 = script:WaitForChild("Attack2")
local Attackanim3 = script:WaitForChild("Attack3")
local Attackanim4 = script:WaitForChild("Attack4")
--Database
local database = {
AttackTrack1 = Humanoid:LoadAnimation(Attackanim1),
AttackTrack2 = Humanoid:LoadAnimation(Attackanim2),
AttackTrack3 = Humanoid:LoadAnimation(Attackanim3),
AttackTrack4 = Humanoid:LoadAnimation(Attackanim4),
}
--Fetching Nearest Player
while wait(0.1) do --It constantly looks to get the closest player
local NearestPlayer
local Nearest = math.huge
for i, q in pairs(workspace:GetDescendants(Players:GetDescendants())) do
if q.Name == "Humanoid" and q.Parent ~= Character then
local magni = (Character.HumanoidRootPart.Position - q.Parent.HumanoidRootPart.Position).Magnitude
if magni <= Nearest then
Nearest = magni
NearestPlayer = q.Parent
end
end
end
--Connections
RunService.Heartbeat:Connect(function()
local path = PathFindingService:CreatePath(PathParams);
if NearestPlayer ~= nil then
path:ComputeAsync(Character.HumanoidRootPart.Position, NearestPlayer.HumanoidRootPart.Position)
CurrentTarget = NearestPlayer;
if path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints();
if waypoints and waypoints[2] then
local data = waypoints[2]
if data.Action == Enum.PathWaypointAction.Jump then
Humanoid.Jump = true
end
Humanoid:MoveTo(data.Position)
end
end
end
end)
--NPC will begin to attack whichever player is the nearest
while wait(AttackInterval) do
local myPos, targetPos = Humanoid.RootPart.Position, CurrentTarget.Torso.Position
if (myPos - targetPos).magnitude <= AttackRange then
print("NPC Attacked")
CurrentTarget.Humanoid:TakeDamage(AttackDamage)
local RandomNumber = math.random(1, 4)
local anim = database["AttackTrack" .. RandomNumber]
anim:Play()
atksfx:Play()
end
end
end