Pathfinding Script won't run

Greetings,

I have created a basic pathfinding script for my game which will be expanded upon later. Currently, the script doesn’t work. The script is as follows:

--Variables
local humanoidRootPart = game.Players.LocalPlayer.HumanoidRootPart
local Humanoid = script.Parent:WaitForChild("Humanoid")
local Torso = script.Parent:WaitForChild("LowerTorso")
local PathFindingService = game:GetService("PathfindingService")
local Target = humanoidRootPart.Position
local Path = PathFindingService:CreatePath()
local HumanoidRootPart = script.Parent:WaitForChild("HumanoidRootPart")

local function followPath(destinationObject)
	-- Compute and check the path
	Path:ComputeAsync(HumanoidRootPart.Position, destinationObject.PrimaryPart.Position)
	-- Empty waypoints table after each new path computation
	waypoints = {}
 
	if Path.Status == Enum.PathStatus.Success then
		-- Get the path waypoints and start walking
		waypoints = Path:GetWaypoints()
		-- Move to first waypoint
		currentWaypointIndex = 1
		Humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
	else
		-- Error (path not found); stop humanoid
		Humanoid:MoveTo(HumanoidRootPart.Position)
	end
end
 
local function onWaypointReached(reached)
	if reached and currentWaypointIndex < #waypoints then
		currentWaypointIndex = currentWaypointIndex + 1
		Humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
	end
end
 
local function onPathBlocked(blockedWaypointIndex)
	-- Check if the obstacle is further down the path
	if blockedWaypointIndex > currentWaypointIndex then
		-- Call function to re-compute the path
		followPath(Target)
	end
end
 
-- Connect 'Blocked' event to the 'onPathBlocked' function
Path.Blocked:Connect(onPathBlocked)
 
-- Connect 'MoveToFinished' event to the 'onWaypointReached' function
Humanoid.MoveToFinished:Connect(onWaypointReached)
 
while true do
	followPath(Target)
	wait(0.1)
end

Any help will be greatly appreciated.

1 Like

Is there any output also is it a local or server script?

2 Likes

It is a server script. And now, looking back at the output, it says this: [Workspace.Dummy.Script:2: attempt to index nil with ‘HumanoidRootPart’]

You need to do game.Players.LocalPlayer.Character.HumanoidRootPart

2 Likes

It is now attempting to index nil with ‘Character’

What type of script are you using because local player is not supported by server scripts only client scripts?

I am using a serverside script. Is there any other way to define any player’s humanoidrootpart?

You need to make a way to find players by getting the player that just joined or make a function that finds the nearest player.

1 Like

Try using wait for child on both of them.

local character = game.Players.LocalPlayer:WaitForChild("Character")
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
1 Like

I think this is correct, but something is still wrong. It is attempting to index ‘Wait For Child’
It is as follows:

--Variables
local character = game.Players.LocalPlayer:WaitForChild("Character")
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
local magnitude = (HumanoidRootPart.Position - game.Workspace.Monster.HumanoidRootPart.Position).magnitude
local Monster = game.Workspace.Monster
local Humanoid = Monster:WaitForChild("Humanoid")
local Torso = Monster:WaitForChild("LowerTorso")
local PathFindingService = game:GetService("PathfindingService")
local Target = HumanoidRootPart
local Path = PathFindingService:CreatePath()
local humanoidRootPart = script.Parent:WaitForChild("HumanoidRootPart")

local function followPath(destinationObject)
	-- Compute and check the path
	Path:ComputeAsync(HumanoidRootPart.Position, destinationObject.PrimaryPart.Position)
	-- Empty waypoints table after each new path computation
	waypoints = {}
 
	if Path.Status == Enum.PathStatus.Success then
		-- Get the path waypoints and start walking
		waypoints = Path:GetWaypoints()
		-- Move to first waypoint
		currentWaypointIndex = 1
		Humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
	else
		-- Error (path not found); stop humanoid
		Humanoid:MoveTo(humanoidRootPart.Position)
	end
end
 
local function onWaypointReached(reached)
	if reached and currentWaypointIndex < #waypoints then
		currentWaypointIndex = currentWaypointIndex + 1
		Humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
	end
end
 
local function onPathBlocked(blockedWaypointIndex)
	-- Check if the obstacle is further down the path
	if blockedWaypointIndex > currentWaypointIndex then
		-- Call function to re-compute the path
		followPath(Target)
	end
end
 
-- Connect 'Blocked' event to the 'onPathBlocked' function
Path.Blocked:Connect(onPathBlocked)
 
-- Connect 'MoveToFinished' event to the 'onWaypointReached' function
Humanoid.MoveToFinished:Connect(onWaypointReached)
 
while true do
	if magnitude >= 50 then
		followPath(Target)
	end
	wait(0.1)
	print("Looped!!!")
end

I didn’t realize but you put this as a local script. Make it into a script, and define the character you want in the workspace.
Pathfinding

This is a regular script. I only said I was using a serverside script.

U can’t use LocalPlayer in server scripts. Since u are moving NPC’s u need to either use a Relative Path or a Absolute path to the NPC

Can I define the value in a local script and transfer that back into the serverside script? Or is this not possible that way?

U don’t want to add pathfinding to normal players right? U also have to know the player u are going to request this from. And well. Thats pointless

I can see, but in theory, this method should work. Think of it like this:

The local script finds the player, and get’s the magnitude of the player to the npc, and if it is under a certain amount, say 50, it would fire a remote event and trigger the npc to go after the player.

See what I’m saying?

Now. A exploiter will fire that event. I mean, why do u want to have the npc to follow you. But still.

You don’t need to define a player to use pathfinding, you need to use a humanoid.

So, instead of checking the humanoidrootpart, I would just check the humanoid?

You still need to know the position of the HumanoidRootPart