PathfindingService Help

So, I’ve been experimenting with pathfinding service for roughly a day now, and I’m struggling to figure out how to make a “smartish-dumbish” NPC. This is because I’ve read that smart NPCs tend to lag in large amounts, and I need a large number of NPCs for my game. And if it’s too dumb, it will fall off the map. I’ve looked into many tutorials and searched the dev forum, no help.

My question is how I can modify my script(below) so that it detects if the NPC will fall or not, or if they can get over or not. I’ve read about raycasting, but I’m very new at it, so it was really hard to understand.

Here’s a video of my problem:

Ass you can see, it tries to jump up to it but it cannot, and it just falls straight off.

How would I be able to make my NPC smarter? (I need it so that it wouldn’t fall off and it wouldn’t jump if I cannot jump over)

Here is my script:

local PFS = game:GetService("PathfindingService")

local Humanoid = script.Parent.Humanoid
local HRP = script.Parent.HumanoidRootPart

local pathArgs = {
	["AgentHeight"] = 5.75,
	["AgentRadius"] = 2.6,
	["AgentCanJump"] = true
}

function Move()
	local Path = PFS:CreatePath(pathArgs)
	Path:ComputeAsync(HRP.Position, HRP.Position + Vector3.new(math.random(-50,50), 0, math.random(-50,50)))
	
	local WayPoints = Path:GetWaypoints()
	
	for i, waypoint in pairs(WayPoints) do
			local Part = Instance.new("Part")
			Part.Position = waypoint.Position
			Part.Size = Vector3.new(1,1,1)
			Part.Anchored = true
			Part.CanCollide = false
			Part.Parent = workspace
			
		end
	
	if Path.Status == Enum.PathStatus.Success then
	
		for i, waypoint in pairs(WayPoints) do
			Humanoid:MoveTo(waypoint.Position)
			
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			end
			
			Humanoid.MoveToFinished:Wait()
		end
	end
end

while wait(3) do
	Move()
end
1 Like

I think you need to confirm that the position of the waypoint is not outside of your arena bounds before using it to Move.

How would I do that? Because my arena is not square, more oblong and circular. I know how raycasting works, but not how to script it. I just need to detect if there is void under the waypoint.

Does this help you

1 Like

I’m sorry it took me so long to reply, I was trying to figure the rays out with the thread you gave me. And raycasting was much easier than I thought(as of now)! And wow, Got so many problems fixed! But, I ran into a problem before this even started, is there a way so that it faces the direction it’s going in? Thank you! The reason I need this is because I’m casting a ray in from of the feet to see if there is any obstruction, and if there is, it would jump.
:smiley:

I believe you can use the players CFrame LookVector. Do a search and see what you get.

No, what I mean is to set the NPC’s look vector to whatever direction it’s going. For example, if the NPC were to go east, then it would face east. I know how to use lookVector, but the NPC is always 90 degrees to the right. How would I be able to make the NPC look in the direction it is facing?

Here is a hint.
Please try a search in your favourite browser like this
roblox article rotate npc
and see if there is anything that may help.
If you include article before what you are looking for you increase the chance of getting some example code.

Yes, I did search before for a long time, but, the humanoid is supposed to move the NPC’s look vector to whatever direction it is facing, but for me, it’s not:

That’s why I’m asking, all the parts orientations are 0,0,0
As you can see in the video, the NPC is moving sideways

You could try to use Custom Character Editor, to establish the joints and attachments of the character.

If that doesn’t work, maybe you could make your own lookvector script, it’s not that hard.