Pathfinding AI Moving back and Forth

Hello, I’m using Simple Path Module Script and even though im using the module right the AI keeps moving towards the player then twitching back and start moving toward again. I’ve seen other posts about this but they aren’t related to my specific problem. There is multiple AI and they should only target players. The AI that appear in workspace with this script are clones from the actual dummy in Replicated Storage.
API: SimplePath
This is my third time trying to get an answer to this problem please help :slight_smile:

--Import the module so you can start using it
local ServerStorage = game:GetService("ServerStorage")
local SimplePath = require(ServerStorage.SimplePath)

local dummy = script.Parent
local maxDistance = 1000000


function getClosestPlayer()
	local nearestPlayer, nearestDistance
	for _, player in pairs(game.Players:GetPlayers()) do
		local character = player.Character
		local distance = player:DistanceFromCharacter(dummy.HumanoidRootPart.Position)
		if not character or 
			distance > maxDistance or
			(nearestDistance and distance >= nearestDistance)
		then
			continue
		end
		nearestDistance = distance
		nearestPlayer = player
	end

		return nearestPlayer.Character.HumanoidRootPart


end

--Define npc
local Dummy = script.Parent

--Create a new Path using the Dummy
local Path = SimplePath.new(Dummy)

--Helps to visualize the path
Path.Visualize = true

--Compute a new path every time the Dummy reaches the goal part
Path.Reached:Connect(function()
	Path:Run(getClosestPlayer())
end)

--Dummy knows to compute path again if something blocks the path
Path.Blocked:Connect(function()
	Path:Run(getClosestPlayer())
end)

--If the position of Goal changes at the next waypoint, compute path again
Path.WaypointReached:Connect(function()
	Path:Run(getClosestPlayer())
end)

--Dummmy knows to compute path again if an error occurs
Path.Error:Connect(function(errorType)
	Path:Run(getClosestPlayer())
end)

Path:Run(getClosestPlayer())
2 Likes

I also used the same Pathfinding AI, and I got the character using Player.CharacterAdded. This is what my script looks like:

local ServerStorage = game:GetService("ServerStorage")
local SimplePath = require(ServerStorage.SimplePath)

game:GetService("Players").PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Dummy = script.Parent
		local Goal = Character:WaitForChild("HumanoidRootPart")
		local Path = SimplePath.new(Dummy)

		Path.Visualize = true

		while wait() do
			Path:Run(Goal)
		end
	end)
end)
1 Like

I think the character not loading isn’t actually the problem. The AI only twitches when there are multiple AI.

For some reason, multiple AI using my code works fine. Is it ok if you share a video of what you’re talking about, only if it doesn’t work.

Also, using your code gave me errors, so I’m surprised how you made it work on your end.

robloxapp-20240510-1931426.wmv (3.0 MB)

Changed code similar to yours

--Import the module so you can start using it
local ServerStorage = game:GetService("ServerStorage")
local SimplePath = require(ServerStorage.SimplePath)

--Define npc
local dummy = script.Parent

--Create a new Path using the Dummy
local Path = SimplePath.new(dummy)

--Helps to visualize the path
Path.Visualize = false

local isDestroyed = false

local maxDistance = 100000

function getClosestPlayer()
	local nearestPlayer, nearestDistance
	for _, player in pairs(game.Players:GetPlayers()) do
		local character = player.Character
		local distance = player:DistanceFromCharacter(dummy.HumanoidRootPart.Position)
		if not character or 
			distance > maxDistance or
			(nearestDistance and distance >= nearestDistance)
		then
			continue
		end
		nearestDistance = distance
		nearestPlayer = player
	end
	if nearestPlayer then
		return nearestPlayer.Character.HumanoidRootPart
	end

end

local run = script:WaitForChild("run")
local humanoid = dummy:WaitForChild("Humanoid")
local runAnimation = humanoid:LoadAnimation(run)


while true do
	Path:Run(getClosestPlayer())
end

Btw the game operates by waves so the player will already be loaded in and dummies are just added to the workspace throughout the wave time until the end of a wave

The enemies somehow might be confused with the other enemies, because I can tell that they are moving back and forth to each other. The code still refuses to work on my side (because it returns literally nothing), so try to figure out a way to only have it detect players. Sorry I can’t help, it just doesn’t work on my side no matter what :frowning_face:

1 Like

What error do you get on your side? or does it just not work?

Best thing is to put the pathfinding in the NPC

AND USE SCRIPT.Parent not using game.workspacr…

This Path.run(getCloserPlayer() looks wrong also

It says something along the lines of ‘Invalid Vector3 or BasePart’. I have no idea why that occurs.

Btw,Use CFrame would be better and not using Position.

Unless your code is like thiz

EXAMPLE:

Player.HuamnoidRootPart.CFrame = CFrame.new(part.Position)

I used @speedb0y1290’s code, and it returns the HumanoidRootPart of the nearest player. But for some reason, when I print the function, it prints literal nothing. I couldn’t figure out how to fix it.

Show me your code then.I will want to check yours because there are errors.

Bruh what is this why that code is like this

I dont hink this line is correct

Nearestdistance and distance >= nearestdistance

Isnt it like this

(NEARESTdistance += distance) >= nearestdistance

I did use script.Parent also the Path:Run(getclosestPlayer()) returns a part needed for the path to run to

What about this module? It should 100% work

local module = {}

function module.Get(position)
	local closest = math.huge
	local current = nil
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		if player.Character then
		local dist = player:DistanceFromCharacter(position)
		if dist < closest and dist ~= 0 then
			closest = dist
			current = player
		end
		else
		continue
	end
	return current.Character.HumanoidRootPart
end
		end

return module

I’ve tried everything I have no idea why my code doesn’t work