Pathfinding ai bug

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

A really good pathfinding AI for enemies

  1. What is the issue? Include screenshots / videos if possible!

The ai makes the rig rotate strangely, no other problems

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Nope

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local hrt=script.Parent.HumanoidRootPart
local hum=script.Parent.Humanoid
local pfs=game:GetService("PathfindingService")

local anim=hum:LoadAnimation(script.attack)

db=false

local function FindTarget()
	local target
	local distance
	local maxdistance=600
	
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Character then
			local hrp=v.Character:FindFirstChild("HumanoidRootPart")
			local distance=(hrt.Position-hrp.Position).magnitude
			if distance<maxdistance then
				target=hrp
				maxdistance=distance
			end
			if distance<5 then
				if db==false then
					anim:Play()
					target.Parent.Humanoid.Health=0
					db=true
					wait(game.Players.RespawnTime)
					db=false
				end
			end
		end
	end
	return target
end

local path=pfs:CreatePath({
	AgentCanJump=true,
	AgentCanClimb=true
})

local function follow(target)
	path:ComputeAsync(hrt.Position, target.Position)
	local wp=path:GetWaypoints()
	for i,v in wp do
		hum:MoveTo(v.Position)
		task.wait()
	end
end

while task.wait() do
	local target=FindTarget()
	if target then
		follow(target)
	end
end
2 Likes

I would try to increase the WaypointSpacing and to debug add small coloured parts to demonstrate the path created.

local function follow(target)
	path:ComputeAsync(hrt.Position, target.Position)
	local wp=path:GetWaypoints()
	for i,v in wp do
		hum:MoveTo(v.Position)
		hum.MoveToFinished:Wait()
	end
end

if i do movetofinished it wont be as good as it was, ill send results after a minute

this is because of the frequency of new paths being created

you wait for the humanoid to finish its current path before creating another one, which is why it’s like that

.MoveToFinished:Wait() yields until the humanoid is done moving to the specified position, where task.wait() waits 1 frame

the fix to this is to just put the follow function in a coroutine, you can do this by doing task.spawn(follow, target)

Note that movetofinish has a stutter, it would not be suitable.

Ah, wait I am stupid.

Basically for such a thing you need to move the rig to the player directly rather than using pathfinding service to do the computing.

So in simple terms:

  • Cast a ray before you use pathfinding service and around every 5th node towards the player to see if the player is in direct sight.
  • Use a while loop and use NPC.Humanoid:MoveTo(Player.RootPart.Position).
  • Problem solved.
1 Like

how do i cast a “ray”? i never actually knew what a ray is in scripting

And also, sorry for late reply, didnt check forum…

Use google. Google raycasting on roblox and t would show you how to do it.

1 Like

Hu- How do you use Google for raycasting? (nvm i have 0 iq i didnt read the sentence properly :pray: )

1 Like

Get reading.

1 Like

bruh i just found out u dont even need pathfinding for following a player…
i just did:

while task.wait() do
local target=FindTarget()
if target then
hum:MoveTo(target.Position, target)
end
end

no errors, no looking away, perfect ai.

Yes but the issue is that now if you move behind an object, the AI breaks. I recommend using my method.