So I was making a tds game and I added pathing to my enemies but then they desided to skip some of the parts of the waypoints. Here is some proof robloxapp-20211219-2131155.wmv (2.5 MB)
Here is my path script:
function mob.Move(enemy, map)
local humanoid = enemy.Humanoid
local waypoints = map.WayPoints
for waypoint=1, #waypoints:GetChildren() do
humanoid:MoveTo(waypoints[waypoint].Position)
humanoid.MoveToFinished:Wait()
end
end
If I where you in this senerio, I wouldn’t do that. I would do it in ipairs instead.
function mob.Move(enemy, map)
local humanoid = enemy.Humanoid
local waypoints = map.WayPoints
for i, waypoint in ipairs(waypoints:GetChildren()) do
humanoid:MoveTo(waypoint.Position)
print("Moving to "..waypoint)
humanoid.MoveToFinished:Wait()
end
end
It may be an issue with humanoid.MoveToFinished:Wait(). I would try changing it to wait(5) first to see if that is the issue, but doing so will obviously not be a permanent solution.
local serverStorage = game:GetService("ServerStorage")
local mob = {}
function mob.Move(enemy, map)
local humanoid = enemy.Humanoid
local waypoints = map.WayPoints
for waypoint=1, #waypoints:GetChildren() do
humanoid:MoveTo(waypoints[waypoint].Position)
humanoid.MoveToFinished:Wait()
end
end
function mob.Spawn(enemyTy, quantity, sDelay, map)
for i=1, quantity do
local enemyExists = serverStorage.Enemies:FindFirstChild(enemyTy)
if enemyExists then
task.wait(sDelay)
local newEnemy = enemyExists:Clone()
newEnemy.HumanoidRootPart.CFrame = map.Start.CFrame
newEnemy.Parent = workspace.Enemies
coroutine.wrap(mob.Move)(newEnemy, map)
else
warn("There is no existing:"..enemyTy.."(Error: Enemy does not exist)")
end
end
end
return mob
You should probably switch from MoveToFinished to a Heartbeat repeat loop.
repeat
game:GetService("RunService").Heartbeat:Wait()
until (enemy.HumanoidRootPart.Position - waypoints[waypoint].Position).Magnitude <= 5
This works with magnitude, so I recommend putting the waypoints as close as possible to the ground so the Y axis doesn’t affect it. There’s an easy way to fix it but you can just put the waypoints on the ground instead.
I’d try changing the direction they’re moving to inside the repeat loop, this way you can make sure they will always try to go towards the waypoint.
repeat
game:GetService("RunService").Heartbeat:Wait()
humanoid:MoveTo(waypoints[waypoint].Position)
until (enemy.HumanoidRootPart.Position - waypoints[waypoint].Position).Magnitude <= 5
Lower the amount of magnitude. Instead of 5 put, I don’t know, 2 or 3?
Also another tip I can give is to add invisible walls around the paths so they cannot get out of them.