Ai doesnt move without giving an error

I wanted to make an ai from a tutorial,it doesnt give an error or something but
when i run it the ai doesn’t move at all,it just stands in place.
How can i make it walk?
Heres the script:

local HRP = script.Parent.HumanoidRootPart
local Humanoid = script.Parent.Humanoid
local PathFindingService = game:GetService("PathfindingService")
local target = nil
local function ComputePath()
	local RetriesLeft = 20
	local RetryInterval = 3
	
	
	
	for i = RetriesLeft,1,-1 do
		
	local Path = PathFindingService:CreatePath()
	Path:ComputeAsync(HRP.Position,target.Position)
	
	if Path.Status == Enum.PathStatus.Success then
			return Path
	else
			warn("Couldnt compute a path,retrying...")
			task.wait(RetryInterval)
	end
end		
	error("Path failed to compute.")
end

local function WalkToTarget()
	local Path = ComputePath(HRP.Position,target.Position)
	local Waypoints = Path:GetWaypoints()
	local CurrentWaypointIndex = 2
	
	for _, point:Waypoints in ipairs(Waypoints) do
		local Part = Instance.new("Part")
		Part.Anchored = true
		Part.CanCollide = false
		Part.Material = Enum.Material.Neon
		Part.Color = Color3.fromRGB(255,255,255)
		Part.Position = point.Position
		Part.Parent = workspace
		
		
	end
	
	Humanoid:MoveTo(Waypoints[CurrentWaypointIndex].Position)
	if Waypoints[CurrentWaypointIndex].Action == Enum.PathWaypointAction.Jump then
		Humanoid.Jump = true
	end
	
end

target = workspace.Part
ComputePath()
WalkToTarget()

Heres the tutorial i used: How To Use Roblox Pathfinding Service 2.0

Thanks in advance.

Is it giving any errors in the output console?
Also, can you show your hierarchy, such as where this script is located in the workspace?

Its in the dummy model
it doesnt give any errors,its in the title

1 Like

I tried your code, and it didn’t work, I am not super familiar with Pathfinding, so I couldn’t really discover what was wrong, however…

This code does work

local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local path = PathfindingService:CreatePath()

--local player = Players.LocalPlayer
--local character = player.Character
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

local TEST_DESTINATION = workspace.Part.Position-- Vector3.new(100, 0, 100)

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection

local function followPath(destination)
	-- Compute the path
	local success, errorMessage = pcall(function()
		path:ComputeAsync(character.PrimaryPart.Position, destination)
	end)

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

		-- Detect if path becomes blocked
		blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			-- Check if the obstacle is further down the path
			if blockedWaypointIndex >= nextWaypointIndex then
				-- Stop detecting path blockage until path is re-computed
				blockedConnection:Disconnect()
				-- Call function to re-compute new path
				followPath(destination)
			end
		end)

		-- Detect when movement to next waypoint is complete
		if not reachedConnection then
			reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
				if reached and nextWaypointIndex < #waypoints then
					-- Increase waypoint index and move to next waypoint
					nextWaypointIndex += 1
					humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
				else
					reachedConnection:Disconnect()
					blockedConnection:Disconnect()
				end
			end)
		end

		-- Initially move to second waypoint (first waypoint is path start; skip it)
		nextWaypointIndex = 2
		humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
	else
		warn("Path not computed!", errorMessage)
	end
end

followPath(TEST_DESTINATION)

It is a slightly modified (made to works as a server script inside the dummy model) version of the script on this page.

https://create.roblox.com/docs/mechanics/pathfinding

So maybe that can help you figure out what is going wrong with yours.

1 Like

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