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