Pathfinding help

Like the humanoid root part? The npc or player?

1 Like

its the waypoint position???

1 Like

What line? Sorry I thought the object is nil.

1 Like

its probably caused by the script not being able to find last waypoint or something

1 Like

if waypoints[#waypoints].Position ~= plrhrp then

1 Like

It doesnt exeist the waypoint
Eeeeered

1 Like

how would it not exist it finds the last waypoint tho

1 Like

gtg to sleep :confused: my dad tell me to sleep

2 Likes

Try this out:

local pfs = game:GetService("PathfindingService")
local path = pfs:CreatePath()

local success, errors = pcall(function()
	path:ComputeAsync(v.HumanoidRootPart.Position, plrhrp)
end)

if success then
	if path.Status ~= Enum.PathStatus.Success then
		return
	end
	
	local waypoints = path:GetWaypoints()
	
	if waypoints[#waypoints].Position ~= plrhrp then
		--do stuff
	end
else
	warn("ERROR: " .. tostring(errors))
end
1 Like

A table inside a table could that be the problem?

1 Like

Honestly I don’t think I can help the best I can do is

for _,waypoint in ipairs(#waypoints) do
   if waypoint.Position ~= plrhrp then
      —Do stuff
   end
end
2 Likes

What do you mean by that?

(ignore char)

1 Like

Isn’t waypoints a table?

waypoints[#waypoints]
1 Like

If you’re trying to detect whether the target is moving away from the calculated end-point of the Pathfinding, I suggest doing something like this instead:

if (waypoints[#waypoints].Position - plrhrp).Magnitude > 5 then
	-- do stuff
end

-- OR THIS IF YOU HAVE THE PLAYER OBJECT (THIS IS LESS INTENSIVE THAT MAGNITUDE)
if plr:DistanceFromCharacter(waypoints[#waypoints].Position) > 5 then
	-- do stuff
end

In my experience with AIs, I’ve personally found issues with checking whether the player is SPECIFICALLY at the end-point because a single flinch or movement of a player would make the AI have to recalculate again which is pretty memory-expensive.

1 Like

# is a length operator. And array[#array] is the most simplest way of getting the last/end value of a array. In most languages, arrays start with 0 instead of 1 (which is what Lua does).

And no, you are not putting a table inside a table because you are adding # which returns the length of a table, not itself.

Here’s an example:

local tbl = {41,"owo",412,14,25,24,254,23}

print(#tbl)--8

print(tbl[#tbl]) --23
print(tbl[8])--23
2 Likes

im tryna detect if the last waypoint’s position is equal to the plrhrp

1 Like

there are no errors but the position not changing

1 Like

found the real problem but idk how fix https://gyazo.com/e42e9beb45f187bc8a85b5a6b924ad5b

1 Like

What exactly are we looking at here?

1 Like

its uh me printing waypoint[#waypoint].Positiob

1 Like