Pet not smooth on server

Hello, I was working on a pet following system but for some reason when I play it’s not as smooth as when it’s from a local script but I want players to see everytone’s pets. Here is how it looks from a server script:


My code (server script):

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        local character = char
        local hrp = character:WaitForChild("HumanoidRootPart")
        local parts = {}
        local numberOfParts = 8
        for _ = 1, numberOfParts do
            table.insert(parts, Instance.new("Part"))
        end
        for i, part in pairs(parts) do
            part.Anchored = true
            part.CanCollide = false
            part.Parent = workspace
        end
        local fullCircle = 2 * math.pi
        local radius = 10
        local function getXAndZPositions(angle)
            local x = math.cos(angle) * radius
            local z = math.sin(angle) * radius
            return x, z
        end

        game:GetService('RunService').Heartbeat:Connect(function()
            for i, part in pairs(parts) do
                local angle = i * (fullCircle / #parts)
                local x, z = getXAndZPositions(angle)
                local position = (hrp.CFrame * CFrame.new(x, drift, z)).p
                local lookAt = hrp.Position
                part.CFrame = part.CFrame:Lerp(CFrame.new(position, lookAt), .1)
            end
        end)
    end)
end)

The code for generating parts around the player is from the solution of this post:

Thank you!

This is caused by replication latency (or, if you prefer, “lag”)

It takes a few dozen milliseconds (or more! Latency on wi-fi for example can be as bad as 500ms) for the Server to “realize” that the Player on a given Client has moved, and another dozen millliseconds (or more!!) for the Server to tell all of its Clients (the other Players) the updated position of all those Parts.

There’s no way to “solve” it, per se, but you can work around it in a number of ways. My solution for this is to not render the Pets on the Server at all, but instead have the Server tell each Client which pets every Player is using, and having each Client handle the pets entirely locally.

The replication delay will still be in effect, e.g. Players pets won’t update immediately when changed (and more notably, the Positions of all other Players will be slightly out of sync with the server while in motion, but this is true no matter what you do!), but while in motion, since the Clients are handling all the pets, it will look smooth.

2 Likes