The green ones stay on path due to having speed being 7 and they stay in the middle of the path. The others with speed much lower run off the path which is incredibley frustrarting. Do I need to add a script to fix this? Maybe edit the current script? Heres the current script:
local PhysicsService = game:GetService("PhysicsService")
local serverStorage = game:GetService("ServerStorage")
local bindables = ServerStorage:WaitForChild("Bindables")
local updateBaseHealthEvent = bindables:WaitForChild("UpdateBaseHealth")
local mob = {}
function mob.Move(mob, map)
local humanoid = mob:WaitForChild("Humanoid")
local waypoints = map.WayPoints
for waypoint=1, #waypoints:GetChildren() do
humanoid:MoveTo(waypoints[waypoint].Position)
humanoid.MoveToFinished:Wait()
end
mob:Destroy()
updateBaseHealthEvent:Fire(humanoid.Health)
end
function mob.Spawn(name, quantity, map)
local mobExists = serverStorage.Mobs:FindFirstChild(name)
if mobExists then
for i=1, quantity do
task.wait(0.5)
local newMob = mobExists:Clone()
newMob.HumanoidRootPart.CFrame = map.Start.CFrame
newMob.Parent = workspace.Mobs
newMob.HumanoidRootPart:SetNetworkOwner(nil)
for i, object in ipairs(newMob:GetDescendants()) do
if object:IsA("BasePart") then
PhysicsService:SetPartCollisionGroup(object, "Mob")
end
end
newMob.Humanoid.Died:Connect(function()
task.wait(0.5)
newMob:Destroy()
end)
coroutine.wrap(mob.Move)(newMob, map)
end
else
warn("Requested mob does not exist", name)
end
end
return mob
I’m pretty sure humanoid:MoveTo() gives up on moving after 7 or 8 seconds if it hasn’t reached their destination yet, so then your script would make them move diagonally because they weren’t at their destination.
When the MoveToFinished event is called, check if the mob is close to the destination. If it isn’t, try moving them again until they are. Also since this looks like a Tower Defense type of game, make sure not to move the enemy or hurt the base if they are dead.
for waypoint=1, #waypoints:GetChildren() do
humanoid:MoveTo(waypoints[waypoint].Position)
humanoid.MoveToFinished:Wait()
end
To this:
for _, point in pairs(waypoints:GetChildren()) do
repeat
humanoid:MoveTo(point.Position)
humanoid.MoveToFinished:Wait()
if not humanoid.Parent or humanoid.Health == 0 then return end -- Stop move function if humanoid is dead
until (humanoid.RootPart.Position - point.Position).Magnitude < 0.25 -- increase for more room for error
end