Optimizing client-server rendering

Hi! Im making an ant simulator, and i found some kind of problem. Since im rendering everything on the client and using remotes to update data on the client i had around 50+ kb/s with ~100 ants rendering.My main task is to reduce recv from 50 kb/s to ~5-7 kb/s. Also i had like 17 ms of CPU usage

Here is some of my scripts:

Server:

local Ants = {
    Storage = {}
}

local Event = Instance.new("RemoteEvent",game.ReplicatedStorage)

function Ants.Generate(Client)
    return table.insert(Ants.Storage,1,setmetatable({
        Position = CFrame.new(),
        Primary = Client.Character.PrimaryPart,
        Random = CFrame.new(math.random(-8,8),0,math.random(-8,8)),
        Moving = false
    },{
        __index = Ants
    }))
end

function Ants:Move(FinalPosition,Delta)
	local Distance = (self.Position.p - FinalPosition.p).Magnitude

    local Speed = 14 * Delta
    local Alpha = math.min(Speed / Distance, 1)

    self.Position = self.Position:Lerp(FinalPosition,Alpha)
    self.Moving = true
end

function Ants:GeneratePacket()
    return {
        Vector2int16.new(math.floor(50 * self.Position.X + 0.5),math.floor(50 * self.Position.Z + 0.5)),
    }
end

function Ants:Simulate(Delta)
    local Object = self.Primary:GetPivot() * self.Random
    
    self:Move(Object,Delta)
end

function Ants.SendPacket(Delta)
    local Packet = {}

    for ID, Ant in Ants.Storage do
        Ant:Simulate(Delta)

        if Ant.Moving then
            Ant.Moving = false

            Packet[ID] = Ant:GeneratePacket()
        end
    end

    if #Packet ~= 0 then
        Event:FireAllClients(Packet)
    end

    return Packet
end

game:GetService("RunService").Heartbeat:Connect(Ants.SendPacket)

return Ants

Client:

local Ants = {}

function Ants.Render(Packet)    

    for ID, Ant in Packet do

        local AntModel = workspace:FindFirstChild(ID)

        local Position = CFrame.new(Ant[1].X / 50,2,Ant[1].Y / 50)

        if not AntModel then

            local Model = game.ReplicatedStorage.Ant:Clone()

            Model.Parent = workspace

            Model.Name = ID

            Model:PivotTo(Position)

            AntModel = Model

            return AntModel

        else

            AntModel:PivotTo(Position)

        end

    end

end

game.ReplicatedStorage:WaitForChild("RemoteEvent",3).OnClientEvent:Connect(Ants.Render)

return Ants
1 Like

Try reducing the amount of times you send the ants over to the client, maybe reduce it to about 20 or 10, and have the client interpolate it on their side, this should reduce the data being sent and also increase performance on the server by a good amount.

A good optimization on the client would be to use BulkMoveTo, or to use octrees to cull ants when not being looked at / too far away. Wind Shake: High performance wind effect for leaves and foliage

Sorry for a late response, hope this helps

2 Likes