How do i improve this code using Lerp instead of MoveTo?

So basically I’m making a tower defense game but using moveto is really laggy and unoptimized

local ServerStorage = game:GetService("ServerStorage")
local PhysicsService = game:GetService("PhysicsService")
local Events = game.ReplicatedStorage.Events
local TakeDamage = Events.TakeDamage

local mob = {}

function mob.Move(mob, map)
	local humanoid = mob:WaitForChild("Humanoid")
	local waypoints = map.Waypoints

	for waypoint=1, #waypoints:GetChildren() do
		mob.MovingTo.Value = waypoint
		repeat
			humanoid:MoveTo(waypoints[waypoint].Position)
		until humanoid.MoveToFinished:Wait()
	end

	mob:Destroy()

	if not (mob.Name == "Arrow") then
		map.Base.Humanoid:TakeDamage(humanoid.Health)
		TakeDamage:FireAllClients()
	end
end

function mob.Spawn(name, quantity, map)
	local mobExists = ServerStorage.Mobs:FindFirstChild(name)

	if mobExists then
		for i=1, quantity do
			wait(0.5)
			local newMob = mobExists:Clone()
			newMob.HumanoidRootPart.CFrame = map.Start.CFrame
			newMob.Parent = workspace.Mobs
			newMob.HumanoidRootPart:SetNetworkOwner(nil)

			local movingTo = Instance.new("IntValue")
			movingTo.Name = "MovingTo"
			movingTo.Parent = newMob

			for i, object in ipairs(newMob:GetDescendants()) do
				if	object:IsA("BasePart") then
					object.CollisionGroup = "Mob"
				end
			end

			newMob.Humanoid.Died:Connect(function()
				local DeathScript = newMob:FindFirstChild("Death")
				if not DeathScript then
					task.wait(0.01)
					newMob:Destroy()
				elseif DeathScript then
				newMob.HumanoidRootPart.Anchored = true
				task.wait(4)
				newMob:Destroy()
				end
			end)

			coroutine.wrap(mob.Move)(newMob, map)
		end

	else
		warn("Requested mob does not exist", name)
	end
end

return mob```

Theres a lot of ways you could do it. Are you asking how to use Lerp to move a model?

Yeah, I’m asking how to use lerp to move a zombie for example and the speed depends on the humanoid’s walk speed

Alright

-- to lerp a model its pretty easy
ZombieModel:PivotTo(StartWaypointCFrame:Lerp(NextWaypointCFrame,Percent))

-- I think the only hard part is doing the math for percent
-- so that the zombie moves at a constant speed

what if there is a lot of waypoints, how am I gonna lerp through all of them

ooh yeah
Well I would just keep track of what waypoint the npc was on
example:

Zombie = {
	Waypoint = 1,
	Percent = 0.243,
	Speed = 2,
	Health = 99.2,
	MaxHealth = 100,
-- maybe waypoints are stored here or something idk
-- this is how id probably set it up but,
-- just as long as waypoints and percent is stored somewhere
}

and while moving the zombie Id add to percent
when percent >= 1
next waypoint and add leftover percent and update position

how do I modify the script I sent using that

Im not sure how id modify it, but you could just update the for loop

local ostime = os.clock() -- use this to keep track of time

change it to a repeat or while
and every update find the difference in time and add percent based on the distance

-- also check for the end of the waypoints
if p > = 1 then
  p -=   1
  waypoint += 1
end
start:lerp(end,percent)