Can't seem to make a Pathfinding AI

Hello,
So I’m working on a horror type game pretty similar to The Rake. I did alot of stuff just can’t seem to make the AI for the monster. Not much of the tutorials helped or I might’ve missed something.
Can someone help maybe?
Thanks.

1 Like

Roblox has a pathfinding service

Already tried myself, didn’t work

What exactly is the problem? Are no waypoints being created when you tried it?

Idk exactly but basically, couldn’t find the nearest player and then chase the player

Try printing out the waypoints that are returned

The pathfinding works just, how to find the nearest player? I tried using magnitude since idk how to use raycast yet

1 Like
local monsterPos = ...  -- should be the pos of the HumanoidRootPart of the monster
local nearestPlayer, nearestDistance
for _, player in pairs(game:GetService('Players'):GetPlayers()) do
  -- Calculate distance between player & monster
  local distance = nearestDistance = (monsterPos - player.Character.HumanoidRootPart.Position).Magnitude
  -- The first player in the loop
  if (nearestPlayer == nil) then
    nearestPlayer, nearestDistance = player, distance
  else
    -- The current distance is less than the last therefore this player is closer
    if (distance < nearestDistance) then
      nearestPlayer, nearestDistance = player, distance
    end
  end
end
1 Like

Definitely will try this, but It’s late night so I’ll go sleep.
Thanks for the help!

1 Like

This is a basic use of path finding service so it could help u!


local pathfindingservice = game:GetService(“PathfindingService”)
local human = script.Parent.Humanoid
local NPC = script.Parent
local path = pathfindingservice:CreatePath()
path:ComputeAsync(script.Parent.HumanoidRootPart.Position, game.Workspace.EndPart.Position)
if path.Status == Enum.PathStatus.Success then

local nodes = path:GetWaypoints()
for i,v in pairs(nodes) do
	print(i)
	if v.Action == Enum.PathWaypointAction.Jump then
		human.Jump = true
	end
	human:MoveTo(v.Position)
	human.MoveToFinished:Wait()
end

end

1 Like

Try using @GnomeCode’s Opensource Teddy script it has waypoints and chasing player functions
link-Patrolling Teddy Bear of Death - Roblox
make sure watch his tutorial it explained parts of the script

Hi, sorry for not responding for so long.
If all of my scripts work well, I’ll count this as the solution.
Also thanks to @kinglol123468 and others who helped!

I’ll try that aswell, thanks for the help