- What do you want to achieve?
Smooth npc movement and interaction as seen in the first part of the video below,
To stop npc pathfinding from randomly freezing after they switch targets after 4-5 attempts
I tried messing with timings, with how moveto() works as it has a timeout from my knowledge, which appears to occur as they freeze for that timeout (7 seconds roughly) But nothing seemed to work, i dont see any timing issues or conflictions with the rest of my code so I must be missing something.
Video on issue (first 15 secs fine):
function Knob.Run(enemy)
local hum = enemy:WaitForChild("Humanoid")
local root = enemy:WaitForChild("HumanoidRootPart")
local detectPlayerRange = enemy:GetAttribute("detectplayer") or 12
local buildBox = Vector3.new(6,6,6)
while hum.Health > 0 do
local playerChar = getClosestPlayer(enemy, detectPlayerRange)
if playerChar then
AttackPlayer(enemy, playerChar)
continue
end
local targetBuilding = findClosestBuilding(enemy)
if not targetBuilding or not targetBuilding.PrimaryPart then
task.wait(0.2)
continue
end
local path = PathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 6,
AgentCanJump = true
})
path:ComputeAsync(root.Position, targetBuilding.PrimaryPart.Position)
if path.Status == Enum.PathStatus.Success then
for _, waypoint in ipairs(path:GetWaypoints()) do
local marker = Instance.new("Part")
marker.Shape = Enum.PartType.Ball
marker.Material = Enum.Material.Neon
marker.Color = Color3.fromRGB(0, 255, 0)
marker.Size = Vector3.new(0.6, 0.6, 0.6)
marker.Anchored = true
marker.CanCollide = false
marker.Position = waypoint.Position
marker.Parent = workspace
-- Auto cleanup after 5 seconds to prevent clutter
game.Debris:AddItem(marker, 5)
end
for _, waypoint in ipairs(path:GetWaypoints()) do
hum:MoveTo(waypoint.Position)
hum.MoveToFinished:Wait()
local playerChar = getClosestPlayer(enemy, detectPlayerRange)
if playerChar then
AttackPlayer(enemy, playerChar)
task.wait(0.1)
--path:ComputeAsync(root.Position, targetBuilding.PrimaryPart.Position)
break
end
local buildInBox = getBuildingInBox(enemy, buildBox)
if buildInBox then
attackBuilding(enemy, buildInBox)
task.wait(0.1)
--path:ComputeAsync(root.Position, targetBuilding.PrimaryPart.Position)
break
end
--task.wait(0.1)
end
end
task.wait(0.01)
end
end
return Knob
Above is the main script for calculating pathfinding, The attackplayer and attack building functions also have moveto() inside, but only waits the duration of 0.05 secs + about 0.7secs but thats based on the attack/ attack speed of the enemy. Let me know if you want to see the entire script incase those are the issues.
Im not great with pathfinding so if anyone knows threads with better bulletproof pathfinding let me know