I haven’t done any pathfinding scripting before today. I’ve watched a couple yt vids and have implemented the basics into an NPC. It works until the destination of the NPC is reached then nothing else happens. Would anyone check it out and possibly send a fixed version? Would be much appreciated!
2 Likes
ok judging from the way you script i guess that you’re new to roblox scripting there was a lot of errors in your code but I fixed your pathfinding code this should work
local PathFinding = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local Jeff = script.Parent
local JeffHumanoid = Jeff:WaitForChild("Humanoid")
local JeffTheKillerScript=script;
repeat wait(0)until JeffTheKillerScript and JeffTheKillerScript.Parent and JeffTheKillerScript.Parent.ClassName=="Model"and JeffTheKillerScript.Parent:FindFirstChild("Head")and JeffTheKillerScript.Parent:FindFirstChild("Torso");
local JeffTheKiller=JeffTheKillerScript.Parent;
function raycast(Spos,vec,currentdist)
local hit2,pos2=game.Workspace:FindPartOnRay(Ray.new(Spos+(vec*.05),vec*currentdist),JeffTheKiller);
if hit2~=nil and pos2 then
if hit2.Name=="Handle" and not hit2.CanCollide or string.sub(hit2.Name,1,6)=="Effect"and not hit2.CanCollide then
local currentdist=currentdist-(pos2-Spos).magnitude;
return raycast(pos2,vec,currentdist);
end;
end;
return hit2,pos2;
end;
function RayCast(Position,Direction,MaxDistance,IgnoreList)
return game:GetService("Workspace"):FindPartOnRayWithIgnoreList(Ray.new(Position,Direction.unit*(MaxDistance or 999.999)),IgnoreList);
end;
local JeffTheKillerHumanoid;
for _,Child in pairs(JeffTheKiller:GetChildren())do
if Child and Child.ClassName=="Humanoid"and Child.Health~=0 then
JeffTheKillerHumanoid=Child;
end;
end;
local AttackDebounce=false;
local JeffTheKillerKnife=JeffTheKiller:FindFirstChild("Knife");
local JeffTheKillerHead=JeffTheKiller:FindFirstChild("Head");
local JeffTheKillerHumanoidRootPart=JeffTheKiller:FindFirstChild("HumanoidRootPart");
local WalkDebounce=false;
local Notice=false;
local MusicDebounce=false;
local NoticeDebounce=false;
local ChosenMusic;
function FindNearestBae()
local NoticeDistance=200;
for _,TargetModel in pairs(game:GetService("Workspace"):GetChildren())do
local IsPlayer = Players:GetPlayerFromCharacter(TargetModel)
local JeffHumanoidRootPart = script.Parent:FindFirstChild("HumanoidRootPart")
if not JeffHumanoidRootPart then
return nil
end
if TargetModel:IsA("Model") and IsPlayer then
local Distance = (JeffHumanoidRootPart.Position - TargetModel:WaitForChild("HumanoidRootPart").Position).Magnitude
if Distance <= NoticeDistance then
print("Found Target")
return TargetModel
end
end
--if JeffTheKillerScript and JeffTheKiller and JeffTheKillerHumanoid and JeffTheKillerHumanoid.Health~=0 and TargetModel.className=="Model"and TargetModel~=JeffTheKiller and TargetModel.Name~=JeffTheKiller.Name and TargetModel:FindFirstChild("Torso")and TargetModel:FindFirstChild("Head")then
-- local TargetPart=TargetModel:FindFirstChild("Torso");
-- local FoundHumanoid;
-- for _,Child in pairs(TargetModel:GetChildren())do
-- if Child and Child.ClassName=="Humanoid"and Child.Health~=0 then
-- FoundHumanoid=Child;
-- end;
-- end;
-- if TargetModel and TargetPart and FoundHumanoid and FoundHumanoid.Health~=0 and(TargetPart.Position-JeffTheKillerHumanoidRootPart.Position).magnitude<NoticeDistance then
-- TargetMain=TargetPart;
-- NoticeDistance=(TargetPart.Position-JeffTheKillerHumanoidRootPart.Position).magnitude;
-- local hit,pos=raycast(JeffTheKillerHumanoidRootPart.Position,(TargetPart.Position-JeffTheKillerHumanoidRootPart.Position).unit,500)
-- if hit and hit.Parent and hit.Parent.ClassName=="Model"and hit.Parent:FindFirstChild("Torso")and hit.Parent:FindFirstChild("Head")then
-- if TargetModel and TargetPart and FoundHumanoid and FoundHumanoid.Health~=0 and(TargetPart.Position-JeffTheKillerHumanoidRootPart.Position).magnitude<9 and not AttackDebounce then
-- spawn(function()
-- AttackDebounce=true;
-- local SwingAnimation=JeffTheKillerHumanoid:LoadAnimation(JeffTheKiller:FindFirstChild("Swing"));
-- local SwingChoice=math.random(1,2);
-- local HitChoice=math.random(1,3);
-- SwingAnimation:Play();
-- SwingAnimation:AdjustSpeed(1.5+(math.random()*0.1));
-- if JeffTheKillerScript and JeffTheKiller and JeffTheKillerKnife and JeffTheKillerKnife:FindFirstChild("Swing")then
-- local SwingSound=JeffTheKillerKnife:FindFirstChild("Swing");
-- SwingSound.Pitch=1+(math.random()*0.04);
-- SwingSound:Play();
-- end;
-- wait(0.3);
-- if TargetModel and TargetPart and FoundHumanoid and FoundHumanoid.Health~=0 and(TargetPart.Position-JeffTheKillerHumanoidRootPart.Position).magnitude<4 then
-- FoundHumanoid:TakeDamage(34);
-- if HitChoice==1 and JeffTheKillerScript and JeffTheKiller and JeffTheKillerKnife and JeffTheKillerKnife:FindFirstChild("Hit1")then
-- local HitSound=JeffTheKillerKnife:FindFirstChild("Hit1");
-- HitSound.Pitch=1+(math.random()*0.04);
-- HitSound:Play();
-- elseif HitChoice==2 and JeffTheKillerScript and JeffTheKiller and JeffTheKillerKnife and JeffTheKillerKnife:FindFirstChild("Hit2")then
-- local HitSound=JeffTheKillerKnife:FindFirstChild("Hit2");
-- HitSound.Pitch=1+(math.random()*0.04);
-- HitSound:Play();
-- elseif HitChoice==3 and JeffTheKillerScript and JeffTheKiller and JeffTheKillerKnife and JeffTheKillerKnife:FindFirstChild("Hit3")then
-- local HitSound=JeffTheKillerKnife:FindFirstChild("Hit3");
-- HitSound.Pitch=1+(math.random()*0.04);
-- HitSound:Play();
-- end;
-- end;
-- wait(0.1);
-- AttackDebounce=false;
-- end);
-- end;
-- end;
-- end;
--end;
end;
end;
while wait(0)do
local TargetPoint=JeffTheKillerHumanoid.TargetPoint;
local Blockage,BlockagePos=RayCast((JeffTheKillerHumanoidRootPart.CFrame+CFrame.new(JeffTheKillerHumanoidRootPart.Position,Vector3.new(TargetPoint.X,JeffTheKillerHumanoidRootPart.Position.Y,TargetPoint.Z)).lookVector*(JeffTheKillerHumanoidRootPart.Size.Z/2)).p,JeffTheKillerHumanoidRootPart.CFrame.lookVector,(JeffTheKillerHumanoidRootPart.Size.Z*2.5),{JeffTheKiller,JeffTheKiller})
local Jumpable=false;
if Blockage then
Jumpable=true;
if Blockage and Blockage.Parent and Blockage.Parent.ClassName~="Workspace"then
local BlockageHumanoid;
for _,Child in pairs(Blockage.Parent:GetChildren())do
if Child and Child.ClassName=="Humanoid"and Child.Health~=0 then
BlockageHumanoid=Child;
end;
end;
if Blockage and Blockage:IsA("Terrain")then
local CellPos=Blockage:WorldToCellPreferSolid((BlockagePos-Vector3.new(0,2,0)));
local CellMaterial,CellShape,CellOrientation=Blockage:GetCell(CellPos.X,CellPos.Y,CellPos.Z);
if CellMaterial==Enum.CellMaterial.Water then
Jumpable=false;
end;
elseif BlockageHumanoid or Blockage.ClassName=="TrussPart"or Blockage.ClassName=="WedgePart"or Blockage.Name=="Handle"and Blockage.Parent.ClassName=="Hat"or Blockage.Name=="Handle"and Blockage.Parent.ClassName=="Tool"then
Jumpable=false;
end;
end;
if JeffTheKillerScript and JeffTheKiller and JeffTheKillerHumanoid and JeffTheKillerHumanoid.Health~=0 and not JeffTheKillerHumanoid.Sit and Jumpable then
JeffTheKillerHumanoid.Jump=true;
end;
end;
if JeffTheKillerScript and JeffTheKiller and JeffTheKillerHead and JeffTheKillerHumanoidRootPart and JeffTheKillerHead:FindFirstChild("Jeff_Step")and (JeffTheKillerHumanoidRootPart.Velocity-Vector3.new(0,JeffTheKillerHumanoidRootPart.Velocity.y,0)).magnitude>=5 and not WalkDebounce and JeffTheKillerHumanoid and JeffTheKillerHumanoid.Health~=0 then
spawn(function()
WalkDebounce=true;
local FiredRay=Ray.new(JeffTheKillerHumanoidRootPart.Position,Vector3.new(0,-4,0));
local RayTarget,endPoint=game:GetService("Workspace"):FindPartOnRay(FiredRay,JeffTheKiller);
if RayTarget then
local JeffTheKillerHeadFootStepClone=JeffTheKillerHead:FindFirstChild("Jeff_Step"):Clone();
JeffTheKillerHeadFootStepClone.Parent=JeffTheKillerHead;
JeffTheKillerHeadFootStepClone:Play();
JeffTheKillerHeadFootStepClone:Destroy();
if JeffTheKillerScript and JeffTheKiller and JeffTheKillerHumanoid and JeffTheKillerHumanoid.Health~=0 and JeffTheKillerHumanoid.WalkSpeed<17 then
wait(0.4);
elseif JeffTheKillerScript and JeffTheKiller and JeffTheKillerHumanoid and JeffTheKillerHumanoid.Health~=0 and JeffTheKillerHumanoid.WalkSpeed>17 then
wait(0.15);
end
end;
WalkDebounce=false;
end);
end;
local MainTarget=FindNearestBae();
local MainTargetHumanoid = nil
if MainTarget then
MainTargetHumanoid = MainTarget:FindFirstChild("Humanoid")
end
--if not MainTarget and not MusicDebounce then
-- spawn(function()
-- MusicDebounce=true;
-- repeat wait(0);if JeffTheKillerScript and JeffTheKiller and ChosenMusic then ChosenMusic.Volume=ChosenMusic.Volume-0.01;else break;end;until ChosenMusic.Volume==0 or ChosenMusic.Volume<0;
-- if ChosenMusic then
-- ChosenMusic.Volume=0;
-- ChosenMusic:Stop();
-- end;
-- ChosenMusic=nil;
-- MusicDebounce=false;
-- end);
--end;
if MainTarget then
print("Found Target")
Notice=true;
NoticeDebounce = true
if JeffTheKillerScript and JeffTheKiller and JeffTheKillerHumanoid and JeffTheKillerHumanoid.Health~=0 then
--if MainTarget and MainTargetHumanoid and MainTargetHumanoid.Health~=0 and(MainTarget.Position-JeffTheKillerHumanoidRootPart.Position).magnitude>5 then
-- JeffTheKillerHumanoid.WalkSpeed=20;
--elseif MainTarget and MainTargetHumanoid and MainTargetHumanoid.Health~=0 and(MainTarget.Position-JeffTheKillerHumanoidRootPart.Position).magnitude<5 then
-- JeffTheKillerHumanoid.WalkSpeed=0.004;
--end;
print("Create Path")
local Path = PathFinding:CreatePath()
Path:ComputeAsync(Jeff.HumanoidRootPart.Position, MainTarget.HumanoidRootPart.Position)
if Path.Status == Enum.PathStatus.Success then
local Nodes = Path:GetWaypoints()
for i, v in pairs(Nodes) do
wait()
local Mark = Instance.new("Part", workspace)
Mark.Name = "Mark"
Mark.Anchored = true
Mark.CanCollide = false
Mark.Material = Enum.Material.Neon
Mark.Position = v.Position
Mark.Size = Vector3.new(0.5, 0.5, 0.5)
end
for i,v in pairs (Nodes) do
JeffHumanoid:MoveTo(v.Position)
if v.Action == Enum.PathWaypointAction.Jump then
JeffHumanoid.Jump = true
end
JeffHumanoid.MoveToFinished:wait()
end
end
else
end;
else
Notice=false;
NoticeDebounce=false;
local RandomWalk=math.random(1,150);
if JeffTheKillerScript and JeffTheKiller and JeffTheKillerHumanoid and JeffTheKillerHumanoid.Health~=0 then
JeffTheKillerHumanoid.WalkSpeed=10;
if RandomWalk==1 then
JeffTheKillerHumanoid:MoveTo(game:GetService("Workspace"):FindFirstChild("Terrain").Position+Vector3.new(math.random(-2048,2048),0,math.random(-2048,2048)),game:GetService("Workspace"):FindFirstChild("Terrain"));
end;
end;
end;
end;
Thanks. Although he still isn’t attacking, which is what I was having issues with.
oh, then I’ll rewrite your code
Great. If possible would you be able to detect the quickest route for the NPC ?
I need help with this aswell, I just want the jeffthekiller script to have pathfinder and I can’t find out how
Could I get the code aswell, I really need this.