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