How should you prepare the pet model and its attachments?
Use one clean Model with a designated root part and a bottom-center Attachment that defines where the pet meets the ground. This gives every pet the same movement interface even when their meshes have different sizes or pivots.
Place the original model in ReplicatedStorage/PetModels, then name its invisible root part PetRoot and assign that part as the model’s PrimaryPart. If you are starting with an imported asset, follow the site’s guide to adding models in Roblox Studio and inspect unfamiliar assets before keeping any included scripts; the Roblox pet models checklist covers what to review.
Add an Attachment named FollowAttachment inside PetRoot. Move it to the bottom center of the pet, with its forward direction matching the direction the pet should face. The script will align this attachment with the desired position, so a tall dragon and a short cat can share the same controller without separate height calculations.
Keep the pet’s decorative parts inside the model. Weld them to PetRoot if the model will ever use physics, but an anchored cosmetic pet can be moved as a complete model with PivotTo(). Roblox’s Creator Hub documentation for models says PivotTo() transforms a model and all its descendant PVInstance objects so that its pivot reaches the supplied CFrame.
Before scripting, give every BasePart these settings:
Anchored = trueCanCollide = falseCanTouch = falseCanQuery = false, unless the part must participate in another raycastMassless = true
These settings make the pet a visual companion rather than a physical obstacle. Damage, bonuses, collection, and other gameplay effects should remain separate from the visible model.
How do you build the client-side pet follow script in Luau?
Clone the pet locally and move it during RunService.PreRender, using the player’s HumanoidRootPart as the reference. Put this LocalScript in StarterPlayerScripts and keep the template named Cat under ReplicatedStorage/PetModels.
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")
local player = Players.LocalPlayer
local template = ReplicatedStorage:WaitForChild("PetModels"):WaitForChild("Cat")
local clientPets = Workspace:FindFirstChild("ClientPets") or Instance.new("Folder")
clientPets.Name = "ClientPets"
clientPets.Parent = Workspace
local pet = template:Clone()
pet.Parent = clientPets
local petRoot = pet.PrimaryPart or pet:FindFirstChild("PetRoot", true)
assert(petRoot and petRoot:IsA("BasePart"), "Pet requires a PetRoot")
pet.PrimaryPart = petRoot
for _, descendant in pet:GetDescendants() do
if descendant:IsA("BasePart") then
descendant.Anchored = true
descendant.CanCollide = false
descendant.CanTouch = false
descendant.CanQuery = false
descendant.Massless = true
end
end
local followAttachment = petRoot:WaitForChild("FollowAttachment")
local pivotToAttachment = pet:GetPivot():ToObjectSpace(followAttachment.WorldCFrame)
local FOLLOW_DISTANCE = 4
local SIDE_OFFSET = 2
local HOVER_HEIGHT = 0.15
local FOLLOW_SPEED = 10
local TELEPORT_DISTANCE = 35
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude
rayParams.IgnoreWater = false
rayParams.RespectCanCollide = true
local currentPivot = pet:GetPivot()
local function getFollowOrigin(root)
local attachment = root:FindFirstChild("PetFollowOrigin")
if not attachment then
attachment = Instance.new("Attachment")
attachment.Name = "PetFollowOrigin"
attachment.Position = Vector3.new(0, 0, FOLLOW_DISTANCE)
attachment.Parent = root
end
return attachment
end
RunService.PreRender:Connect(function(deltaTime)
local character = player.Character
local root = character and character:FindFirstChild("HumanoidRootPart")
local humanoid = character and character:FindFirstChildOfClass("Humanoid")
if not root or not humanoid or humanoid.Health <= 0 then
return
end
rayParams.FilterDescendantsInstances = {character, pet}
local origin = getFollowOrigin(root)
local desired = origin.WorldPosition + root.CFrame.RightVector * SIDE_OFFSET
local rayOrigin = desired + Vector3.new(0, 8, 0)
local groundHit = Workspace:Raycast(
rayOrigin,
Vector3.new(0, -20, 0),
rayParams
)
local groundY = groundHit and groundHit.Position.Y or desired.Y - 3
local flatLook = Vector3.new(
root.CFrame.LookVector.X,
0,
root.CFrame.LookVector.Z
).Unit
local moving = humanoid.MoveDirection.Magnitude > 0.05
local bob = moving and math.sin(Workspace.DistributedGameTime * 7) * 0.12 or 0
local attachmentPosition = Vector3.new(
desired.X,
groundY + HOVER_HEIGHT + bob,
desired.Z
)
local targetAttachment = CFrame.lookAt(
attachmentPosition,
attachmentPosition + flatLook
)
local targetPivot = targetAttachment * pivotToAttachment:Inverse()
local distance = (currentPivot.Position - targetPivot.Position).Magnitude
if distance > TELEPORT_DISTANCE then
currentPivot = targetPivot
else
local alpha = 1 - math.exp(-FOLLOW_SPEED * deltaTime)
currentPivot = currentPivot:Lerp(targetPivot, alpha)
end
pet:PivotTo(currentPivot)
end)
Roblox documents PreRender as a client-side event that runs before each frame is drawn and receives the elapsed time since the previous frame. The RunService reference also warns that rendering waits for work in this event, so the callback should remain short.
How do you make pet movement smooth without changing its speed across devices?
Use delta-time-based interpolation, maintain a trailing offset, and add only subtle procedural motion. The expression 1 - math.exp(-speed * deltaTime) gives the interpolation a similar response at different frame rates instead of moving a fixed percentage per frame.
The script applies that expression before calling CFrame:Lerp(). According to the Roblox CFrame reference, Lerp() interpolates both position and rotation, with its alpha running from zero at the current transform to one at the target.
Tune FOLLOW_SPEED before adding more animation. A low value creates a loose, floaty companion; a higher value makes the pet hold formation tightly. Keep the bob amplitude small enough that the FollowAttachment still appears connected to the terrain, and give each pet a different phase when several are equipped:
local bob = math.sin(Workspace.DistributedGameTime * 7 + petIndex * 0.8) * 0.12
For richer idle behavior, animate bones, Motor6D joints, or an AnimationController inside the pet instead of rotating the whole model aggressively. The follow controller should decide where the pet goes, while the animation system decides how it walks, flies, or rests.
How do you fix pet jitter, wall clipping, and sudden teleporting?
Smooth one stable target, raycast for terrain and obstacles, and reserve teleporting for large discontinuities such as respawns. Do not alternate between physics forces, direct CFrame changes, and PivotTo() on the same pet.
Jitter commonly appears when the server and client both reposition a pet or when an unanchored assembly competes with the follow script. Use one movement owner: an anchored client-rendered model for cosmetic pets, or a deliberately designed physics controller for interactive pets.
The downward ray in the example keeps the attachment near the floor. Roblox’s raycasting documentation states that Workspace:Raycast() returns a RaycastResult when it hits an eligible part or Terrain cell, and that filtering can exclude selected descendants. Excluding the character and pet prevents the ray from finding either one as its own floor.
To reduce wall clipping, cast another ray from the player toward the proposed follow position. If it hits a wall, shorten the offset so the pet stops slightly in front of the hit point, then perform the downward ground ray from that adjusted position. Avoid using a long navigation path for every frame; pathfinding is better reserved for companions that must navigate independently rather than remain in a nearby formation.
The distance check handles true discontinuities. When the player respawns, teleports, or crosses a large gap, immediately place the pet at its new target instead of letting it fly across the map. Raise or lower TELEPORT_DISTANCE to fit the scale and speed of your experience.
How can several equipped pets follow in a formation?
Assign every equipped pet a stable slot and convert that slot’s local offset through the character’s CFrame. A compact grid or arc prevents pets from stacking while keeping the formation predictable.
This helper produces rows of up to three pets behind the player:
local function getFormationOffset(index, total)
local petsPerRow = 3
local row = math.floor((index - 1) / petsPerRow)
local column = (index - 1) % petsPerRow
local countInRow = math.min(petsPerRow, total - row * petsPerRow)
local centeredColumn = column - (countInRow - 1) / 2
return Vector3.new(centeredColumn * 3, 0, 4 + row * 3)
end
For each pet, replace the fixed follow origin with:
local offset = getFormationOffset(petIndex, #equippedPets)
local desired = root.CFrame:PointToWorldSpace(offset)
Keep slot order stable by using the server’s equipped-slot index rather than sorting pets by a value that can change. Increase row and column spacing from each model’s bounds if your roster mixes very small and very large companions. Looking at how established Roblox games use pets can also help you choose between a tight simulator-style grid and a looser adventure-game formation.
How should pet replication and mobile performance be optimized?
Replicate equipped-pet state, not a new pet transform from the server every frame, and let each client render the cosmetic formation. Keep gameplay rewards and equip validation authoritative on the server.
One practical structure is a server-created EquippedPets Folder under each Player, containing numbered StringValue slots whose values are approved pet IDs. Every client observes those replicated slots and creates local visual models for all nearby players. This is a design recommendation: it avoids sending a continuous stream of cosmetic transforms while ensuring that each client can still see other players’ companions.
Never trust a client’s claim that it owns or equipped a pet. Roblox’s client-server security guidance recommends validating the type, context, permissions, and rate of client requests on the server. The server should check inventory ownership and slot limits before changing replicated equipped state.
For lower-end devices, share calculations where possible. Compute the character transform once per frame, reuse ground results for nearby slots, update distant pets less frequently, and disable particles, shadows, or detailed animations beyond a chosen distance. Do not guess at a universal pet limit: measure the actual models, scripts, effects, and target devices in your experience.
Roblox’s hardware testing guidance recommends an active 10–15 minute mobile session when checking sustained performance and points developers to Performance Stats, the Developer Console, and MicroProfiler captures for diagnosis.
How do you test a Roblox pet system in multiplayer?
Run Studio’s Server & Clients mode with at least two clients and verify what each client sees before publishing. Test respawning, joining late, equipping, unequipping, teleporting, stairs, walls, moving platforms, and slow network conditions.
Roblox’s Studio testing modes documentation says Server & Clients launches one server session and separate client sessions, allowing you to compare how each client sees the others. It supports up to eight simulated clients, although one or two are usually enough for routine checks.
Use this release checklist:
- Each client sees its own pets and the other player’s pets.
- Unequipped models are destroyed and event connections are disconnected.
- A late-joining client reconstructs existing equipped pets.
- Respawning creates a new follow origin without leaving the pet at the old character.
- Pets recover cleanly after a server-authorized teleport.
- Walls, slopes, stairs, and moving platforms do not produce persistent clipping.
- Invalid equip requests are rejected by the server.
- Frame time remains acceptable with the largest supported formation on a real mobile device.
A reliable Roblox Studio companion system separates three responsibilities: the server owns pet inventory and effects, replicated state describes what is equipped, and each client renders smooth movement. That separation keeps the Roblox pet follow script responsive without letting a cosmetic client model decide gameplay outcomes.