Hey, I created a functionality to move my entities on server side and replicate their positions on client.
After a period of time appear a bug which start to tremble the entities…
I’d like to remove this bug and may be optimize my server-client system,
Variant 1:
(Client)
game:GetService("RunService").Heartbeat:Connect(function()
for id, entitySaved in pairs(SavedEntities) do
entitySaved["Model"]:PivotTo(newPath:CalculateUniformCFrame(entitySaved.Progress))
end
end)
(Server)
local totalDistance = EntityMove:calculateTotalDistance(Path)
local serverPart = workspace.ServerSide
RunService.Heartbeat:Connect(function(deltaTime)
for index, newEntity in ipairs(Entities) do
local distanceTraveled = newEntity.Speed * deltaTime
-- Update distance traveled for the entity
newEntity.DistanceTravelled = (newEntity.DistanceTravelled or 0) + distanceTraveled
-- Calculate progress for the entity
local progress = newEntity.DistanceTravelled / totalDistance
if progress >= 1 then
ClientReplicator:FireAllClients("Destroy", tostring(newEntity.EntityId))
table.remove(Entities, index) -- Remove the entity from the table
else
local newCframe = newPath:CalculateUniformCFrame(progress)
serverPart.CFrame = newCframe
ClientReplicator:FireAllClients("UpdateProgress", tostring(newEntity.EntityId), progress)
end
end
end)
Variant 2:
(Client)
game:GetService("RunService").Heartbeat:Connect(function()
for id, entitySaved in pairs(SavedEntities) do
if entitySaved.Cframe then
entitySaved["Model"]:PivotTo(entitySaved.Cframe)
end
end
end)
(Server)
local totalDistance = EntityMove:calculateTotalDistance(Path)
local serverPart = workspace.ServerSide
RunService.Heartbeat:Connect(function(deltaTime)
for index, newEntity in ipairs(Entities) do
local distanceTraveled = newEntity.Speed * deltaTime
-- Update distance traveled for the entity
newEntity.DistanceTravelled = (newEntity.DistanceTravelled or 0) + distanceTraveled
-- Calculate progress for the entity
local progress = newEntity.DistanceTravelled / totalDistance
if progress >= 1 then
ClientReplicator:FireAllClients("Destroy", tostring(newEntity.EntityId))
table.remove(Entities, index) -- Remove the entity from the table
else
local newCframe = newPath:CalculateUniformCFrame(progress)
serverPart.CFrame = newCframe
ClientReplicator:FireAllClients("UpdatePosition", tostring(newEntity.EntityId), newCframe)
end
end
end)
Video example:
From the start
After period of time
I’m fighting with this problem already 3 days. I don’t know how to optimize this to let me at least to move 150 models…