Path Finding For Mobs Breaks When I Put Their Speed To Low, Help?

Hey Devs pretty much my script works until I put the speed of the mobs to be under 7.


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

Thanks for any help I really appreciate it!

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.

1 Like

How would I be able to fix this?

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.

1 Like

Change this code:

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

I had a similar question in a different post. And this was the answer that I got that could be helpful!

1 Like

This was with the path finding service… ^ And could be useful here.

1 Like

So what would the fix be here? Make a bunch more waypoints to account for the slow mobs?

this script breaks after they reach the first waypoint.