"Attempt to index function with CreatePath" bug

I am making a pathfinding script for a game, but I have the NPC move to a certain point first

This is the section of the script

while true do
	for i, plr in pairs(game.Players:GetChildren()) do
		game.ReplicatedStorage.NewObjective:FireClient(plr, '<font color = "#ff0000">- GO TO CLASS!!!</font>')
	end

	local path = pathfind:CreatePath()
	path:ComputeAsync(hrp.Position, Vector3.new(-35.943, -210.411, 1595.615))
	local waypoints = path:GetWaypoints()

	for i,v in waypoints do
		if v.Action == Enum.PathWaypointAction.Jump then
			humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		end
		humanoid:MoveTo(v.Position)
		script.Parent.Humanoid.MoveToFinished:Wait()
	end
	task.wait(10)
	local startTime = os.clock()
	repeat
		local found = everything()
	until found == nil or os.clock() - startTime > 30
	
	local path = pathfind:CreatePath()
	path:ComputeAsync(hrp.Position, Vector3.new(136.655, -209.552, 1451.934))
	local waypoints = path:GetWaypoints()
	
	for i,v in waypoints do
		if v.Action == Enum.PathWaypointAction.Jump then
			humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		end
		player = task.spawn(fnp)
		if player then continue end
		humanoid:MoveTo(v.Position)
		script.Parent.Humanoid.MoveToFinished:Wait()
	end
	local distance = (hrp.Position - waypoints[#waypoints]).Magnitude
	if distance > 6 then continue end
	
	task.wait(math.random(60, 95))
	for i, plr in pairs(game.Players:GetChildren()) do
		game.ReplicatedStorage.NewObjective:FireClient(plr, '- Survive')
	end
end

The bugged line is the first

local path = pathfind:CreatePath()

Screenshot 2024-11-17 003601
This is the error that shows.

I haven’t been able to find any solutions on the DevForum and dont know what to do to fix it. I’ve tried changing the variable name and removing the variable altogether. I copied the code from a different portion of the script, so I don’t understand why it would bug here. There is a lot of pathfinding in the script so it could be that, but I just don’t get how it could be here. If anyone can help, thanks!

shouldn’t it first be ComputeAsync and then CreatePath?

No, like I said, the code works everywhere except for the spot I put it in.

i’m going to ask a stupid question

but i assume pathfind is pathfinding service?

i cant’ see any errors besides the unknown-to-me pathfind variable

Yep, very first line.

local pathfind = game:GetService("PathfindingService")

I will say, I did a little more experimenting and it might have to do with the while true do loop??? That caused every single pathfind to break exactly this way.

i don’t see why that is, but i guess you can try not doing a while true loop

You’re setting the variable pathfind to be equal to a function at some point, which is why the error is saying “attempt to index function with CreatePath”. Could you send the entire script?

You’re right.

local function pathfind(player)
	for i, plr in pairs(game.Players:GetChildren()) do
		game.ReplicatedStorage.NewObjective:FireClient(plr, '<font color = "#ff0000">- GO TO CLASS!!!</font>')
	end

	local path = pathfind:CreatePath()
	path:ComputeAsync(hrp.Position, Vector3.new(-35.943, -210.411, 1595.615))
	local waypoints = path:GetWaypoints()

	for i,v in waypoints do
		if v.Action == Enum.PathWaypointAction.Jump then
			humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		end
		humanoid:MoveTo(v.Position)
		script.Parent.Humanoid.MoveToFinished:Wait()
	end
	task.wait(10)
	local startTime = os.clock()
	
	local path = pathfind:CreatePath()
	path:ComputeAsync(hrp.Position, player.Character:FindFirstChild("HumanoidRootPart").Position)
	local waypoints = path:GetWaypoints()

	for i,v in waypoints do
		if v.Action == Enum.PathWaypointAction.Jump then
			humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		end
		humanoid:MoveTo(v.Position)
		script.Parent.Humanoid.MoveToFinished:Wait()
	end
end
1 Like

Haha. I do very similar stuff all the time and spend hours tracking down a very simple fix.