So basically I made an npc where if you get in it’s hitbox it will start chasing you, however it’s really twitchy and stuff and I’m wondering how I can fix this, here’s footage of it:
Here’s my code:
local hitbox = script.Parent:FindFirstChild("HITBOX")
local pathFindingService = game:GetService("PathfindingService")
local npcHumanoid = script.Parent:FindFirstChild("Humanoid")
local npcTorso = script.Parent:FindFirstChild("Torso")
while true do
wait()
local partsInRegion = workspace:GetPartBoundsInBox(hitbox.CFrame, hitbox.Size, nil)
for i, object in pairs(partsInRegion) do
if object.Parent:FindFirstChild("Humanoid") then
local route = pathFindingService:CreatePath()
route:ComputeAsync(npcTorso.Position, object.Position)
local waypoints = route:GetWaypoints()
for i, waypoint in pairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
npcHumanoid:ChangeState("Jumping")
end
npcHumanoid:MoveTo(waypoint.Position)
npcHumanoid.MoveToFinished:Wait(3)
end
npcHumanoid:MoveTo(object.Position)
end
end
end
There’s also a bug where he will chase you beyond the 3 seconds, anyone know how to fix?
Also, my explorer looks like this:
okay so, here’s my totally perfect pathfinding code
note that the first block is from a module script so don’t worry about the return path
local success, errorMessage = pcall(function()
path:ComputeAsync(startPosition, targetPosition)
end)
if not success then
warn("Pathfinding failed: " .. errorMessage)
return
end
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success and #waypoints > 0 then
for i = 2, #waypoints do
local waypoint = waypoints[i]
if waypoint.Action == Enum.PathWaypointAction.Jump then
Humanoid.Jump = true
end
Humanoid:MoveTo(waypoint.Position)
local moveFinished = Humanoid.MoveToFinished:Wait()
end
return path
end
and then inside the pathfinding script i do this:
task.spawn(function() -- task.spawn so multiple enemies at a time can follow you, needs to be inside an ipairs loop, only do this if you're trying to handle every enemy with one script (which is what i'm doing)
if closestPlayer then
local targetPosition = closestPlayer.Character.Torso.PathfindPart.Position
pathfindingModule.melee(humanoid, targetPosition)
else
humanoid:MoveTo(enemy.HumanoidRootPart.Position)
end
end)
so now if i am not wrong (i probably am) your code should look something like this (do note that i’m only giving you the way to pathfind, no player detection whatsoever):
for i, object in pairs(partsInRegion) do
if object.Parent:FindFirstChild("Humanoid") then
local route = pathFindingService:CreatePath()
route:ComputeAsync(npcTorso.Position, object.Position)
local waypoints = route:GetWaypoints()
-- indentations becuase i'm writing this on the website
if route.Status == Enum.PathStatus.Success and #waypoints > 0 then
for i = 1, #waypoints do
local waypoint = waypoints[i]
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true -- i'm not sure if this is the best way to initiate a jump
end
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
end
end
i can’t guarantee my code is the most optimal but it floats my boat