I have seen this issue before with chasing AI, but all the solutions on the Dev Forum that I try don’t work.
If you don’t watch the video, I’ll just explain it here.
The main issue, which I’m sure most have seen this before, is the AI constantly stuttering to chase the player. I’ve never seen anyone ask this question about why the pathfinding works perfectly fine when the model is walking and not running at a faster speed?
Anyways, I’m hoping to look for a fix that works well and allows the AI to run at it’s normal speed to kill players.
Before anyone says stuff about Network Owner, I already have the Network Owner set to nil, so I don’t know another solution.
Here is the code that I’ve made and used for many years.
--[[ By: xXTerra_X. ]]--
local AiScript=script;
local Subject=AiScript.Parent;
if Subject:FindFirstChild("Thumbnail") then
Subject:FindFirstChild("Thumbnail"):Destroy();
end;
function GetHumanoid(Model)
for _, v in pairs(Model:GetChildren())do
if v:IsA'Humanoid' then
return v;
end;
end;
end;
local SubjectHumanoid=GetHumanoid(Subject);
local SubjectHumanoidRootPart=Subject:FindFirstChild("HumanoidRootPart");
local Pathing=Game:GetService("PathfindingService");
local Targets=Game:GetService("Players");
local NoticeDistance=99999;
local Vars=AiScript:FindFirstChild("Vars");
local Path;
local WayPoint;
local TargetName=nil;
local WanderX,WanderZ=30,30;
local DamageDebounce=false;
function GetTorso(Part)
local Targets=Game.Workspace:GetChildren();
local TargetRoot=nil;
local TargetTorso=nil;
local TargetPlayer=nil;
local FoundHumanoid=nil;
local NoticeDebounce=NoticeDistance;
for i = 1, #Targets do
TargetPlayer=Targets[i];
if TargetPlayer:IsA("Model") and TargetPlayer~=Subject then
FoundHumanoid=GetHumanoid(TargetPlayer);
TargetRoot=TargetPlayer:FindFirstChild("HumanoidRootPart");
if TargetRoot~=nil and FoundHumanoid~=nil and FoundHumanoid.Health>0 and FoundHumanoid.Name~="Zombie" then
if (TargetRoot.Position-Part).magnitude<NoticeDebounce then
TargetName=TargetPlayer.Name;
TargetTorso=TargetRoot;
NoticeDebounce=(TargetRoot.Position-Part).magnitude;
end;
end;
end;
end;
return TargetTorso;
end;
function GetPlayersBodyParts(Test)
local Torso=Test;
if Torso then
local Target=Torso.Parent;
for _, v in pairs(Target:GetChildren())do
if v:IsA("Part") then
return v.Name;
end;
end;
else
return "HumanoidRootPart";
end;
end;
Spawn(function()
while Wait(0)do
for _,V in pairs(Game:GetService("Workspace"):GetDescendants())do
local FoundHumanoid=V:FindFirstChildOfClass("Humanoid");
local FoundHumanoidRootPart=V:FindFirstChild("HumanoidRootPart");
if V:IsA("Model")and FoundHumanoidRootPart and FoundHumanoid and V~=Subject and V.Parent~=Game:GetService("Workspace"):FindFirstChild("Killer Storage") and Subject.Parent==Game:GetService("Workspace"):FindFirstChild("Killer Storage")then
if(FoundHumanoidRootPart.Position-SubjectHumanoidRootPart.Position).Magnitude<=5 then
local MainTarget=FoundHumanoid.Parent;
if not MainTarget:FindFirstChildOfClass("ForceField")then
local TargetPlayer=Game:GetService("Players"):FindFirstChild(MainTarget.Name);
if not TargetPlayer.PlayerGui:FindFirstChild("JSGui")then
local JSGui=AiScript:FindFirstChild("JSGui"):Clone();
JSGui.Parent=TargetPlayer.PlayerGui;
JSGui:FindFirstChild("ImageLable"):FindFirstChild("Sound"):Play();
Vars.Attacking.Value=true;
FoundHumanoid:TakeDamage(99999e99999);
Vars.Attacking.Value=false;
end;
end;
end;
end;
end;
end;
end);
Spawn(function()
while Vars.Wandering.Value==false and SubjectHumanoid.Health > 0 do
Vars.Chasing.Value=false;
Vars.Wandering.Value=true;
local desgx, desgz=SubjectHumanoidRootPart.Position.x+math.random(-WanderX,WanderX), SubjectHumanoidRootPart.Position.z+math.random(-WanderZ,WanderZ);
local function checkw(t)
local ci=3;
if ci>#t then
ci=3;
end
if t[ci]==nil and ci<#t then
repeat ci=ci+1 wait() until t[ci]~=nil;
return Vector3.new(1,0,0)+t[ci];
else
ci=3;
return t[ci];
end;
end;
Path=Pathing:FindPathAsync(SubjectHumanoidRootPart.Position,Vector3.new(desgx,0,desgz));
WayPoint=Path:GetWaypoints();
local connection;
local direct=Vector3.FromNormalId(Enum.NormalId.Front);
local ncf=SubjectHumanoidRootPart.CFrame*CFrame.new(direct);
direct=ncf.p.unit;
local rootr=Ray.new(SubjectHumanoidRootPart.Position,direct);
local phit,ppos=Game.Workspace:FindPartOnRay(rootr,SubjectHumanoidRootPart);
if Path and WayPoint or checkw(WayPoint) then
if checkw(WayPoint) ~= nil and checkw(WayPoint).Action==Enum.PathWaypointAction.Walk then
SubjectHumanoid:MoveTo( checkw(WayPoint).Position);
SubjectHumanoid.Jump=false;
end
if checkw(WayPoint)~=nil and checkw(WayPoint).Action==Enum.PathWaypointAction.Jump then
connection=SubjectHumanoid.Changed:connect(function()
SubjectHumanoid.Jump=true;
end)
SubjectHumanoid:MoveTo(WayPoint[4].Position);
else
SubjectHumanoid.Jump=false;
end
if connection then
connection:Disconnect()
end;
else
for i=3,#WayPoint do
SubjectHumanoid:MoveTo(WayPoint[i].Position);
end;
end;
Wait(math.random(1,2));
Vars.Wandering.Value=false;
end
end)
while Wait() do
local nrstt=GetTorso(SubjectHumanoidRootPart.Position)
if nrstt~=nil and SubjectHumanoid.Health > 0 then
Vars.Wandering.Value=false;
Vars.Chasing.Value=true;
local function checkw(t)
local ci = 3
if ci > #t then
ci = 3
end
if t[ci]==nil and ci<#t then
repeat ci=ci+1 Wait() until t[ci]~=nil;
return Vector3.new(1,0,0)+t[ci];
else
ci = 3;
return t[ci];
end;
end;
Path=Pathing:FindPathAsync(SubjectHumanoidRootPart.Position,nrstt.Position);
WayPoint=Path:GetWaypoints();
local connection;
local direct=Vector3.FromNormalId(Enum.NormalId.Front);
local ncf=SubjectHumanoidRootPart.CFrame*CFrame.new(direct);
direct=ncf.p.unit;
local rootr=Ray.new(SubjectHumanoidRootPart.Position,direct);
local phit,ppos=Game.Workspace:FindPartOnRay(rootr,SubjectHumanoidRootPart);
if Path and WayPoint or checkw(WayPoint) then
if checkw(WayPoint)~=nil and checkw(WayPoint).Action==Enum.PathWaypointAction.Walk then
SubjectHumanoid:MoveTo( checkw(WayPoint).Position);
SubjectHumanoid.Jump=false;
end;
if checkw(WayPoint)~=nil and checkw(WayPoint).Action==Enum.PathWaypointAction.Jump then
connection=SubjectHumanoid.Changed:connect(function()
SubjectHumanoid.Jump=true;
end);
SubjectHumanoid:MoveTo(WayPoint[4].Position);
else
SubjectHumanoid.Jump=false;
end
if connection then
connection:Disconnect();
end;
else
for i=3,#WayPoint do
SubjectHumanoid:MoveTo(WayPoint[i].Position);
end;
end;
Path=nil;
WayPoint=nil;
elseif nrstt==nil then
Vars.Wandering.Value=false;
Vars.Chasing.Value=false;
TtargetName=nil;
Path=nil;
WayPoint=nil;
SubjectHumanoid.MoveToFinished:Wait();
end;
end;
--[[ By: xXTerra_X. ]]--
All help is appreciated and I hope to find a well worked solution. Thanks