[HELP!] AI instantly changing path before reaching destination ):

the title says it all. here is the script:

for a,b in ipairs(script.WAYPOINTS_FOLDER.Value.InCooldownWaypoints:GetChildren()) do
	b.Parent = script.WAYPOINTS_FOLDER.Value.WanderPoints
end



local PFS = game:GetService("PathfindingService")

local Bot = script.Parent.Parent
local RP = Bot:WaitForChild("HumanoidRootPart")
local Human = Bot:WaitForChild("Humanoid")

local Path = PFS:CreatePath({
	AgentRadius = 2,
	AgentHeight = 6,
	AgentCanJump = false,
	AgentMaxSlope = 80,  -- Adjust based on your environment
	AgentCanClimb = false,  -- Adjust based on your environment
})

-- Function to wait for the humanoid to reach its destination

-- Function to follow the waypoints
local function followPath(waypoints)
	for _, waypoint in ipairs(waypoints) do
		Human:MoveTo(waypoint.Position)
	end
end

-- Function to handle pathfinding and movement
local function moveToWaypoint(part)
	Path:ComputeAsync(RP.Position, part.Position)

	if Path.Status == Enum.PathStatus.Success then
		local WPs = Path:GetWaypoints()
		if #WPs > 0 then
			followPath(WPs)  -- Follow the waypoints
		end
	else
		print("Pathfinding failed with status:", Path.Status.Name)
	end
end

-- Main loop
while true do
	local Folder = script.WAYPOINTS_FOLDER.Value.WanderPoints:GetChildren()
	if #Folder >= 1 then
		local Rnum = math.random(1, #Folder)
		local Part = Folder[Rnum]

		--task.spawn(function()
			--Part.Parent = script.WAYPOINTS_FOLDER.Value.InCooldownWaypoints
			--wait(10)
			--Part.Parent = script.WAYPOINTS_FOLDER.Value.WanderPoints
		--end)

		moveToWaypoint(Part)
	else
		print("No waypoints available")
	end

	task.wait(0.1)  -- Short wait before starting the next path
end

use Humanoid.MoveToFinished event Humanoid | Documentation - Roblox Creator Hub

2 Likes
-- Variables --
local Bot = script.Parent.Parent
local RP = Bot:WaitForChild("HumanoidRootPart")
local Human = Bot:WaitForChild("Humanoid")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local path = game.PathfindingService:CreatePath({
	AgentRadius = 4,
	AgentHeight = 6,
	AgentCanJump = false,
	WaypointSpacing = 8, -- Adjust this value to change waypoint spacing
	Costs = {
		DangerZone = math.huge
	}
})
local Character = script.Parent.Parent
local humanoid = Character.Humanoid

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection

local function followPath(destination)
	local success, errormessage = pcall(function()
		path:ComputeAsync(RP.Position, destination)
	end)

	if success and path.Status == Enum.PathStatus.Success then
		waypoints = path:GetWaypoints()

		blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			if blockedWaypointIndex >= nextWaypointIndex then
				blockedConnection:Disconnect()
				followPath(destination)
			end
		end)

		if not reachedConnection then
			reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
				if reached and nextWaypointIndex < #waypoints then
					nextWaypointIndex += 1
					humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
				else
					reachedConnection:Disconnect()
					blockedConnection:Disconnect()
				end
			end)
		end

		nextWaypointIndex = 2
		humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
	else
		warn("Path not Computed!", errormessage)
	end
end

-- Main loop
while true do
	local Folder = script.WAYPOINTS_FOLDER.Value.WanderPoints:GetChildren()
	if #Folder >= 1 then
		local Rnum = math.random(1, #Folder)
		local Part = Folder[Rnum]

		-- Start following the path to the selected waypoint
		followPath(Part.Position)

-- ADD PART TO COOLDOWN NOT WORKING! PUTS ALL INSTANTLY INTO COOLDOWN!

	else
		print("No waypoints available")
	end

	task.wait(0.05)  -- Short wait before starting the next path
end

WHERE DO I PUT THIS HERE? it instantly puts all of the waypoints into cooldown and then errors with no waypoints avaliable!

local function followPath(waypoints)
	for _, waypoint in ipairs(waypoints) do
		Human:MoveTo(waypoint.Position)
human.MoveToFinished:Wait()
	end
end

what i was trying to add to the script is this

	task.spawn(function()
		Part.Parent = script.WAYPOINTS_FOLDER.Value.InCooldownWaypoints
		wait(10)
		Part.Parent = script.WAYPOINTS_FOLDER.Value.WanderPoints
	end)

to add the part into a cooldown folder and also make the humanoid reach destination before chosing and targeting a new one

then still humanoid.MoveToFinished:Wait()

you could try using some sort of variable called pathCompleted and block the humanoid from traversing a new path when that variable is set to false or something

can you insert that in the code so it works?

local function followPath(waypoints)
pathCompleted = false
	for _, waypoint in ipairs(waypoints) do
		Human:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
	end

pathCompleted = true
end
if Path.Status == Enum.PathStatus.Success and pathCompleted then
		local WPs = Path:GetWaypoints()
		if #WPs > 0 then
			followPath(WPs)  -- Follow the waypoints
		end
	else
		print("Pathfinding failed with status:", Path.Status.Name)
	end

idk i can’t be bothered to do it perfectly, here’s an idea of what you’re supposed to do

“wi cant be bothered” what do you mean, if you dont have the intention to help why are you here? if you know the solution, tell it to me and you get your answer market as solution

I’m not going to spoonfeed you, I told you what to do;

  1. Create a boolvalue variable and call it something like pathCompleted

  2. Whenever the NPC tries to compute a new path and move along that path, check if pathCompleted is true or false (depends on how you set it)

  3. If pathCompleted is false, do not compute the path and do not attempt to make the humanoid move along that path.

  4. If pathCompleted is true, compute the path and move the humanoid along that path.

If we just spoonfeed you, you won’t be able to learn anything from it. Its like asking someone to create a game for you, you have no idea what they’re doing but it works (maybe).

wait so does this boolvalue prevent redundant path:ComputeAsync? Sorry if I misunderstood this.

if you tell the code to prevent computing a path if the previous path isn’t completed yet, then yes it does prevent it

if pathCompleted then
path:ComputeAsync()
end

if pathCompleted then
-- walk along the new path
end
1 Like