Im making an npc that wanders around until a player comes nearby, and will then chase the player but it this is what happens when it tries to chase a player
local monster = script.Parent
local humanoid = monster.Humanoid
local hrp = monster.HumanoidRootPart
local nodes = monster.Parent.MovementNodes:GetChildren()
local pathfinding = game:GetService(“PathfindingService”)
local chasing = false
local sight = 50
game:GetService(“RunService”).Heartbeat:Connect(function()
local players = game.Players:GetChildren()
for i = 1, players do
local character = players[i].Character or players[i].CharacterAdded:Wait()
local plrhrp = character:FindFirstChild(“HumanoidRootPart”)
local dis = (hrp.Position - plrhrp.Position).Magnitude
if dis <= sight then
humanoid.WalkSpeed = 20
chasing = true
local path = pathfinding:CreatePath()
path:ComputeAsync(hrp.Position,plrhrp.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
if chasing == true then
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
humanoid:MoveTo(waypoint.Position)
print(“Chasing”)
humanoid.MoveToFinished:Wait()
end
end
else
humanoid.WalkSpeed = 14
chasing = false
end
end
end)
local function Wander()
humanoid.WalkSpeed = 14
local target = nodes[math.random(1,#nodes)]
local path = pathfinding:CreatePath()
path:ComputeAsync(hrp.Position,target.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
if chasing == false then
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
humanoid:MoveTo(waypoint.Position)
print("Wandering")
humanoid.MoveToFinished:Wait()
else
break
end
end
if chasing == false then
task.wait(1)
Wander()
end
local monster = script.Parent
local humanoid = monster:WaitForChild("Humanoid")
local hrp = monster:WaitForChild("HumanoidRootPart")
local nodes = monster.Parent.MovementNodes:GetChildren()
local pathfinding = game:GetService("PathfindingService")
local chasing = false
local sight = 50
game:GetService("RunService").Heartbeat:Connect(function()
local players = game.Players:GetPlayers()
for i = 1, #players do
local character = players[i].Character
if character then
local plrhrp = character:FindFirstChild("HumanoidRootPart")
if plrhrp then
local dis = (hrp.Position - plrhrp.Position).Magnitude
if dis <= sight then
humanoid.WalkSpeed = 20
chasing = true
local path = pathfinding:CreatePath()
path:ComputeAsync(hrp.Position, plrhrp.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
if chasing == true then
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
humanoid:MoveTo(waypoint.Position)
print("Chasing")
humanoid.MoveToFinished:Wait()
else
break
end
end
else
humanoid.WalkSpeed = 14
chasing = false
end
end
end
end
end)
local function Wander()
humanoid.WalkSpeed = 14
local target = nodes[math.random(1,#nodes)]
local path = pathfinding:CreatePath()
path:ComputeAsync(hrp.Position,target.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
if chasing == false then
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
humanoid:MoveTo(waypoint.Position)
print("Wandering")
humanoid.MoveToFinished:Wait()
else
break
end
end
if chasing == false then
task.wait(1)
Wander()
end
end
task.delay(1, Wander)
Try this?
Youre recalculating the path 60 times per second which well might cause jitters, plus some other stuff.
This happens because you are recalculating the path too many times every second, which leads to the Monster NPC constantly only being able to move to the first waypoint that is in the path, and due to server latency delay this first waypoint is often placed behind the character, leading to jitters.
→ Change your code to only recalculate the path every half a second. To compensate the monster should have a higher attack range.
→ Instead of looping through every waypoint in order, I recommend doing this:
for i, waypoint in pairs(waypoints) do
local waypoint = waypoints[i + 2] or waypoints[#waypoints]
if chasing == true then
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
humanoid:MoveTo(waypoint.Position)
print("Chasing")
humanoid.MoveToFinished:Wait()
else
break
end
end
Starting with the third waypoint instead of the first should fix the issue where the path starts behind the character, and you can adjust that number as you want.