Help with synchronization with TD movement

So I have had this problem ever since I have made the concept of this movement system for enemies. So what basically happens is when the game lags on a device or something. The enemies get desynchronized from one another. Ive tried to put tick instead of delta time as you see in the
“progress” variable of the script and it seemed to be more synchronized but when i changed the enemy’s speed to 0, it put it’s position right back to where it started (the waypoint it previously reached). So I need to find out if its possible to synchronize enemies that won’t mess up the code in anyway. Also I don’t know how to create a rotation method that will work for this so for now I set the cframe so that it turns at the end but obesely it’s going to be a instant turn. Can anyone help?

function mob.LerpTo(newMob, target, XZOffset)
	local targetPos = Vector3.new(target.Position.X,target.Position.Y + newMob.Stats.YOffset.Value,target.Position.Z)
	local alpha = 0
	local startCFrame = newMob.HumanoidRootPart.Position
	local loop
	local loopBool = false
	local distance = (newMob.HumanoidRootPart.Position - targetPos).Magnitude
	local startTick = tick()
	
	loopBool = true
	loop = RunService.Heartbeat:Connect(function(deltaTime)
		if newMob and newMob:FindFirstChild("Stats") then
			local speed = newMob.Stats.Speed.Value
			local progress = tick() - startTick
			local relativeSpeed = distance / speed
			local goalCFrame = startCFrame:Lerp(targetPos, alpha)
			newMob.HumanoidRootPart.Position = goalCFrame
			alpha += deltaTime / relativeSpeed
			if alpha >= 1 then
				newMob.HumanoidRootPart.CFrame = target.CFrame * CFrame.new(0,newMob.Stats.YOffset.Value,0)
				loop:Disconnect()
				loopBool = false
			end
		else
			loop:Disconnect()
			loopBool = false
		end
	end)
	
	repeat RunService.Heartbeat:Wait()
	until loopBool == false
end

How about you have one script that moves all the mobs at once, where during each Heartbeat it would loop over all existing mobs and run this script.

Thank you for the reply. I think I forgot to mention this is a module script. This would probably make it have the same result. Also this is only on the server side of things so not client between server things are happening and im just trying to find a simple way of how to keep them synchronized. You probably thought that i want them to be synced with one another but in reality its like being misaligned or like someone said it’s moving a few studs ahead or behind i don’t know.

So it’s lagging on the client due to network fluctuations, but on the server itself it’s stable?

Well on the server it looks like their just floating in a random location and on the client is where it’s actually moving and looks correct (besides the synchronization). I really am not a expert on movements at all.

client in studio:
image
server in studio:
image
and yes i know their aligned but there is just no lag right there, now i could show a better visual of the path arrows doing it since it messes up all the time.

not synced arrow (towards the end of path):
image

synced arrow (when first spawned):
image

So the movement is done on the client, and I would assume that the server sends new positions to the client and then the client moves the enemies to those positions slowly by using the mob.LerpTo() function.

If that’s correct, then why don’t you move the enemies on the server itself so that it auto-replicates their positions, they should always be in sync, and players could experience some desynchronization with the server due to network lag, but it should always be fixed automatically (since they are aligned on the server anyway).
If you don’t want to do that, you could create a global GameTime NumberValue and have it managed by the server, then send spawn info to the client including the GameTime it spawned at such that even if the client experiences some delay, it may compensate for it (given that it already knows the path of the enemy).

Well you see i don’t do any client sided stuff its just roblox doing that automatically. I just don’t know what to do at this point because im pretty sure its something with delta time because i do believe that tick made it more synced and delta time i am pretty sure is really unreliable and the only problem is when the speed is set to like 0 it just reset its position back to the waypoint it previously reached. But the arrows seem like the their still messing up so idk what to do like i said. Also sorry if I don’t know what to say because I am not full mind rn.