How do I make pet follow system

Hey there everyone,

Currently I’m trying to create a pet follow system like the one in pet sim, but I have no idea how they did it. My current code is:

RunService.Heartbeat:Connect(function()
    PetMesh.CFrame = PetMesh.CFrame:Lerp(CFrame.new(targetPosition, playerHorizontalPosition), 0.15)
end)

But the problem with this is that the pet will vibrate back and forth when the player moves, shown in the video below:


You can see that the pet jitters a little bit fowards and a bit backwards when the player walks, but in pet sim the pet walking animation looks like this:

You can see that theres no jittering and its movement is very smooth, I have no idea how to achieve this.
Thanks a lot for the help!

2 Likes

I’d recommend looking through the code within the “Model Following a Player” code sample / free model from Roblox, which is effectively a basic example for a standard pet following system.

When using that resource, the movement of the model is pretty smooth; I briefly showcased this at 1:52 in the following video:

1 Like

Hey there,
the model was nice and I tested it but it wasn’t exactly what I was looking for because sometimes when the player turns the model doesnt follow behind it
Thanks a lot for the help thougfh

2 Likes

Have you tried a tween, and is your script on the server or client

yes Ive tried a tween and it does the same thing, the script is also on the server

No problem; Sorry that it didn’t end up being the solution, though!

I am not very familiar with pet systems but I believe that these sorts of effects are generally handled on the client-side so it’s more responsive and less likely to cause a strain on the server.

If you swap it to being run via a LocalScript and the jitteriness doesn’t immediately go away, consider updating the code within your original post from using Heartbeat to use other events, such as RenderStepped, to see if that makes any difference.

  • Or you could experiment with each of the RunService Events to see which one ends up having the best results for your use case.
1 Like

yeah but if I swap it to a localscript then other players wont be able to see the pets move

replicate it to all clients using :FireAllClients()

wouldnt that lag even more and be less smooth

still looking for help, im currently thinking its a problem with the run service spamming the lerp

Could setting the network owner to the player help?

1 Like

no, since it runs on the client, just on every other client, which is the same as running on the server, you just use the resources locally

the problem is probably coming from the fact that the player seems to be jittering too ? the CFrame is probably calculated at the exact degree so maybe using math.round() or something in the likes would help ?
i would need to see the entire script to be able to solve this properly

try

RunService.Heartbeat:Connect(function()
    PetMesh.CFrame = PetMesh.CFrame:Lerp(CFrame.new(math.round(targetPosition), math.round(playerHorizontalPosition)), 0.15)
end)

mate lerp/tween on the server is just straight laggy, theres nothing you can do about it. If you want it smooth you have to do it on the client.

I understand but it means that other players won’t be able to see the pet moving, and using FireAllClients for every frame seems it would take a much heavier toll on the server

There is aboslutely no toll on the server if the pet movement runs on the client, since your own computer does the calculations. Not the server. You can freely use :FireAllClients without worrying about performance issues. Every FPS game replicates shots using :FireAllClients (or else the shots would lag) and you can imagine how many shots per second happen in a full server, either way the game runs like butter. There is no lag, there will be no performance issues, there will be no huge memory usage. I dont think there is a better solution for your problem, but you choose.

ohh wait I understand now, earlier I thought that it was going to fire :FireAllClients every frame using runservice that was mb

My guess is they created a custom rig with a humanoid and did humanoid:MoveTo()
You’ll probably want them to ignore obstacles somewhat, so teleporting if they get too far away is a good idea.

To get a world position based on your character, you can do
HumanoidRootPart.CFrame:PointToWorldSpace(PetPosition)
Where PetPosition is the the localized position relative to the character. Probably something like Vector3.new(0,0,10). And then for two pets you could use Vector3.new(2,0,10), Vector3.new(-2,0,10)
That way your many pets will know where to go.

Here’s a link for a rig editor:

After creating a custom rig for your pet, you can animate it with a walk animation and idle animation.