How do I replicate enemy movement on client? [Tower defense]

Hello again,
I’m making a tower defense game and i’ve only made enemy pathfinding on the server. I’m still stuck at how to render enemies on client correctly.
I used to render enemies by sending information from the server to the client every 20 heartbeats, however i found this post:

This post shows that i can do it without using a loop to send information. But I still dont understand what to do after reading the post’s solution. (especially when the solution said to workout where the enemy is using the distance)
Here is my code:
Server side [module script] :

local replicatedStorage = game:GetService("ReplicatedStorage")
local serverScriptService = game:GetService("ServerScriptService")

local remotes = replicatedStorage:WaitForChild("GameSystem"):WaitForChild("Remotes")

local gFolder = game.Workspace.Game.Game

local waypoints = gFolder.Waypoints
local enemyRenders = gFolder["PositionRenders [Enemy]"]

local IDHandler = require(serverScriptService.ServerResources.Modules.IDHandler)

local Enemy = {}
Enemy.__index = Enemy

function Enemy.new(speed,health,id)
	local class = setmetatable({},Enemy)

	class.Speed = speed
	class.CurrentHealth = health
	class.ID = id

	class.Died = false
	class.Position = waypoints["1"].CFrame
	class.CurrentWaypoint = 1
	class.CurrentHealth = health
	
	Enemy:Init(class)

	return class
end

function Enemy.Destroy(self)
	self.Died = true
end

function Enemy:Init(self)
	local WPPos : CFrame
	
	--find the enemy value, assign attributes
	local cframe = IDHandler:findEnemyWithID(self.ID, enemyRenders)
	cframe:SetAttribute("Speed", self.Speed)
	cframe:SetAttribute("Health", self.CurrentHealth)
	
	print("set")
	
	--make enemy walk to waypoints
	local function moveToWaypoint()
		if self.CurrentWaypoint > #waypoints:GetChildren() then IDHandler:removeUniqueID(self.ID) cframe:Destroy() Enemy:Destroy(self) return end
		
		WPPos = waypoints[tostring(self.CurrentWaypoint)].CFrame
		cframe.Value = self.Position
		
		local distance = (WPPos - self.Position).Magnitude
		local timeWalking = distance / self.Speed
		
		task.wait(timeWalking)
		
		self.Position = WPPos
		self.CurrentWaypoint += 1
		cframe.Value = self.Position
		
		moveToWaypoint()
	end
	
	task.spawn(moveToWaypoint)
end

return Enemy

Client side [local script]:

task.wait(1)
local replicatedStorage = game:GetService("ReplicatedStorage")
local tweenService = game:GetService("TweenService")

local gameSystem = replicatedStorage:WaitForChild("GameSystem")
local gameFolder = game.Workspace.Game.Game

local modules = gameSystem.Modules
local remotes = gameSystem.Remotes
local assets = gameSystem.Assets

local MobData = require(modules.EnemiesAndTowers.Enemies.MobData)

local hpDisplay = assets.VisualEffects.HealthDisplay

gameFolder["PositionRenders [Enemy]"].ChildAdded:Connect(function(Child : CFrameValue)
	local mob : Model = MobData:RetrieveMobKey(Child.Name, "Model"):Clone()
	mob.Parent = gameFolder.Enemies
	
	local hp = Child:GetAttribute("Health")
	local speed = Child:GetAttribute("Speed")
	
	local old_pos = Child.Value
	
	Child.Changed:Connect(function(value)
		local distance = (value - old_pos).Magnitude
		local timeWalking = distance / speed
		tweenService:Create(mob.PrimaryPart,TweenInfo.new(timeWalking, Enum.EasingStyle.Linear), {CFrame = CFrame.new(value)}):Play()
		
		old_pos = value
	end)
end)

If anyone atleast explain to me what does it mean by “workout where the enemy is using the distance”, or knows what I’m missing, what I’m doing wrong , or if I should revert back to the heartbeat loop method, please let me know, and I’ll try my best to fix it.

So the name of the method being employed in that post you linked is called “client side prediction”.

The client is periodically synchronized to the server, and in between those syncs, the client “predicts” how the units are moving based on their last known position and the velocity they were moving at. The client then assumes that they’re going to continue having that velocity for a while, smoothly interpolating the unit’s position along that trajectory until the next synchronization occurs.

With more advanced predictions, you can further reduce the number of times a synchronization is required. In the most advanced case, assuming the movement is entirely deterministic, the client runs its own local copy of the entire game and synchronizations only occur when an “unpredictable” event occurs, such as a player inputting commands or the server starting a random event.

You do still need to synchronize with the server in a loop, the key is that you only do it when you need to, rather than wasting bandwidth doing it continuously.

So based on my script and your explanation, how i would change my script is:
Whenever an enemy spawns, i save its cframe to the first waypoint, and save the time it spawned. On the client, i render the enemy and make it move to each waypoint like normal.
When the enemy gets stunned (or every status effect that slows down it), i save its current cframe on the client, and reset time.
Is this practical? I’m really sorry for asking further since im not used with these stuff.

Hi, I know I am not providing a solution but I have created TD games in the past. If you would like, review my source code for a Tower Defence game to see if this helps.

Thanks a ton, ill check the game’s source code and later improvise my script if possible.

That’s the gist of it, yes.

Keep in mind that the server will always be very slightly out of sync with the client even under ideal conditions – every time the server sends an authoritative update, you have to reconcile the difference with the client’s game state.

The simplest option is to just teleport the units to their correct positions, but that tends to jump out at players in a bad way. A more clever approach is to only teleport units when the disparity is “significant”, otherwise you could, for example, increase the unit’s speed temporarily as it runs to the corrected position.

I made it render now, thanks everyone.