AI Pathfind Help

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
robloxapp-20240605-0930407.wmv (4.4 MB)


local RepStorage = game:GetService("ReplicatedStorage")
local SimplePath = require(RepStorage.SimplePath)

local module = require(game.ReplicatedStorage.AI.GetClosestPlayer)


--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


while true do
	if Dummy.Humanoid.Health >= 1 then
		local char = SimplePath.GetNearestCharacter(script.Parent.HumanoidRootPart.Position)
		Path:Run(char.HumanoidRootPart)
	end
end

Spawning Script:

local rep = game.ReplicatedStorage
local updateTimers = rep.UpdateTimers
local formatNumber = require(rep.FormatNumbers)
local enemies = game.ReplicatedStorage.Enemy:GetChildren()
local seconds = 60
local wave = 0
local spawns = workspace.Spawns:GetChildren()

local requiredBalls = 2
task.wait(2)
repeat
	task.wait(1)
if seconds ~= 0 and wave ~= 3 then
	seconds -= 1
	updateTimers:FireAllClients(formatNumber.convertToMinutesAndSeconds(seconds))
	local canSpawn = math.random(1,10)
	
	if canSpawn >= 3 then -- 70% chance to spawn
		local enemyToSpawn = enemies[math.random(1, #enemies)]
		local randomSpawnPoint = spawns[math.random(1, #spawns)]
		local enemyClone = enemyToSpawn:Clone()
		
		enemyClone.HumanoidRootPart.Position = randomSpawnPoint.Position
		enemyClone.Parent = workspace
		enemyClone.HumanoidRootPart:SetNetworkOwner(nil)
	end
elseif seconds <= 0 then
	
	-- Next Wave Starts Here
	if workspace.Deposit.CurrentGolds.Value >= requiredBalls then
		wave +=1
	seconds = 60 -- Reset to starting value
	requiredBalls += 5
	else
		for i, v in pairs(game.Players:GetPlayers()) do
			v.Character.Humanoid.Health = 0
		end
	end
end
until wave == 3
2 Likes

30 minutes and still no replies… :frowning:

1 Like

Couple of things you can try. First try setting the “visualize” property to true:

Path.Visualize = true

This will allow you to see where the NPC is trying to go at a specific point in time(the waypoints), which might help you find out why the NPC is “twitching”.

1 Like

I’ve done that. The video shows where they have a straight path to me but they keep going backwards

2 Likes

I had a similar problem in the past using SimplePath, but I don’t remember exactly how I fixed it.
I would also recommend using Runservice for loops instead of “while true do”, considering you may run into issues with lag(especially if there are multiple npcs).

2 Likes

RunService Heartbeat function kinda makes the path visualization act funny but I planned on using collection service when I get the individual script working so it should work but thanks for the suggestion.

1 Like

you could also try adding a cooldown to the loop and see if that changes anything

1 Like

sorry i know its been a while but did you remember how you fixed it?

I forgot to check, my bad.
Is this how you want the NPC to move?
robloxapp-20240612-1012549.wmv (2.0 MB)

yes, pretty much although I would rather there not be an delay on the movement because I wanted it to be smooth not too robotic like.

Okay I made it less robotic, I’ll send you the place file in a minute

1 Like

PathfindingTest.rbxl (67.4 KB)
Lmk if you have questions

1 Like

Thank you so much. I’ll check it when I get the chance.

1 Like

Your game works in the empty baseplate but its not working when I import it into my game, so it must be some other script in my game.

Thats strange, do you have a video of it happening?

robloxapp-20240613-2100513.wmv (2.6 MB)
The third one isn’t moving because i’m doing modifications on it to find the problem

it could be possibly performance issues?

Or because GetNearestCharacter returns any character, not just a player.

From looking at the module it should only get players

function Path.GetNearestCharacter(fromPosition)
	local character, dist = nil, math.huge
	for _, player in ipairs(Players:GetPlayers()) do
		if player.Character and (player.Character.PrimaryPart.Position - fromPosition).Magnitude < dist then
			character, dist = player.Character, (player.Character.PrimaryPart.Position - fromPosition).Magnitude
		end
	end
	return character
end

--Code from module

Interesting, the API doesn’t specify. Are there any other scripts affecting the NPCs?