Movetofinished is delaying

  1. What do you want to achieve? i want to make my ai chase script smooth, not delayed ;-;

  2. What is the issue? My npc is waiting until path is finished, but i want to upgrade the path when target’s position changed, how to do it? :confused:
    btw i captured a video for it: video

  3. What solutions have you tried so far? I didnt found a solution for it in devforum, also i tried many solutions like when target’s position changed, then break the loop, but its breaks loop so its not moving to waypoints when position changed, otherwise its moves to waypoints.

local soldier = script.Parent
local humanoid = soldier.Humanoid
local pathfindingservice = game:GetService("PathfindingService")

soldier.PrimaryPart:SetNetworkOwner(nil)

local function createPath(destination)
	local pathfindingservice = game:GetService("PathfindingService")
	local path = pathfindingservice:CreatePath()
	path:ComputeAsync(soldier.HumanoidRootPart.Position, destination.HumanoidRootPart.Position)
	
	-- this is showing path waypoints so nvm it
	for i, w in pairs(workspace.waypoints:GetChildren()) do
		w:Destroy()
	end
	local waypoints = path:GetWaypoints()
	for i, w in pairs(waypoints) do
		local waypoint = Instance.new("Part")
		waypoint.Name = tostring(table.find(waypoints,w))
		waypoint.Position = w.Position
		waypoint.CanCollide = false
		waypoint.Parent = workspace.waypoints
		waypoint.Shape = "Ball"
		waypoint.Size = Vector3.new(1, 1, 1)
		waypoint.Anchored = true
	end

	return path
end

local function walkTo(destination)
	local path = createPath(destination)
	local waypoints = path:GetWaypoints() 
	for i, w in pairs(waypoints) do
		humanoid:MoveTo(w.Position)
		humanoid.MoveToFinished:Wait()	

	end	
	
end

local function findTarget()
	local targets = workspace.RedTeamSoldiers:GetChildren()
	local maxDistance = math.huge
	local nearestTarget
	if not (targets == nil) then
		for i, t in pairs(targets) do
			local distance = (soldier.HumanoidRootPart.Position - t.HumanoidRootPart.Position).Magnitude
			if distance < maxDistance and t.Humanoid.Health > 0 then
				nearestTarget = t
			end
		end
	else	
		nearestTarget = nil
	end
	return nearestTarget
end


local function patrol()
	local target = findTarget()

	if target then
		walkTo(target)
	end


end

while true do
	patrol()
end

by the way, npc is chasing other npcs, not players :sweat_smile:

once you get the waypoints
local waypoints = path:GetWaypoints()

start moving the npc towards the first waypoint
humanoid:MoveTo(waypoints[1].Position)

and while the npc is walking to the first waypoint start working out a new path
path:ComputeAsync(waypoints[1].Position, destination.HumanoidRootPart.Position)

and then once the npc gets to the first waypoint then start walking to the first waypoint for the next path and keep doing this over and over

now this is computationally heavy because ComputeAsync is a expensive function so you might want to optimize a bit by maybe let the npc walk a few waypoints before computing the next path and you can do a raycast to see if its safe to walk directly towards the other npc without path finding

2 Likes

thank you so much
it worked :slightly_smiling_face:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.