Pet following on the server

local runService = game:GetService(“RunService”)

local playerPets = workspace:WaitForChild(“Pets”)

local circle = math.pi * 2
local plr = game:GetService(“Players”).LocalPlayer
local function getPosition(angle, radius)
local x = math.cos(angle) * radius
local z = math.sin(angle) * radius
return x, z
end

local function positionPets(character, folder, dt)
for i, pet in pairs(folder:GetChildren()) do
local radius = 4+#folder:GetChildren()
local angle = i * (circle / #folder:GetChildren())
local x, z = getPosition(angle, radius)
local _, characterSize = character:GetBoundingBox()
local _, petSize = pet:GetBoundingBox()

	local offsetY = - characterSize.Y/2 + petSize.Y/2
	local sin = (math.sin(15 * time() + 1.6)/.5)+1
	local cos = math.cos(7 * time() + 1)/4
	if pet:FindFirstChild("Attack").Value ~= nil then
		if pet:FindFirstChild("Walks") then
			local position = (pet:FindFirstChild("Attack").Value.PrimaryPart.CFrame * CFrame.new(x, 0, z)).p
			local lookAt = pet:FindFirstChild("Attack").Value.PrimaryPart.Position
			pet:SetPrimaryPartCFrame(pet.PrimaryPart.CFrame:Lerp(CFrame.new(position, lookAt) * CFrame.new(0, 1 +sin, 0) * CFrame.fromEulerAnglesXYZ(0,0,cos),0.1))
		else
			local position = (pet:FindFirstChild("Attack").Value.PrimaryPart.CFrame * CFrame.new(x, 0, z)).p
			local lookAt = pet:FindFirstChild("Attack").Value.PrimaryPart.Position
			pet:SetPrimaryPartCFrame(pet.PrimaryPart.CFrame:Lerp(CFrame.new(position, lookAt) * CFrame.new(0, 3+math.sin(time()*7)/2, 0) * CFrame.fromEulerAnglesXYZ(cos,0,0) ,0.1))
		end 
	else
		if character.Humanoid.MoveDirection.Magnitude > 0 then
			if pet:FindFirstChild("Walks") then
				pet:SetPrimaryPartCFrame(pet.PrimaryPart.CFrame:Lerp(character.PrimaryPart.CFrame * CFrame.new(x, offsetY+sin, z) * CFrame.fromEulerAnglesXYZ(0,0,cos),0.1))
			else
				pet:SetPrimaryPartCFrame(pet.PrimaryPart.CFrame:Lerp(character.PrimaryPart.CFrame * CFrame.new(x, offsetY/2+math.sin(time()*3)+1, z),0.1))
			end 
		else
			if pet:FindFirstChild("Walks") then 
				pet:SetPrimaryPartCFrame(pet.PrimaryPart.CFrame:Lerp(character.PrimaryPart.CFrame * CFrame.new(x, offsetY, z) ,0.1))
			else
				pet:SetPrimaryPartCFrame(pet.PrimaryPart.CFrame:Lerp(character.PrimaryPart.CFrame * CFrame.new(x, offsetY/2+math.sin(time()*3)+1, z) ,0.1))
			end
		end
	end

end

end

runService.RenderStepped:Connect(function(dt)
for _, PlrFolder in pairs(playerPets:GetChildren()) do
local Player = game.Players:FindFirstChild(PlrFolder.Name) or nil
if Player ~= nil then
local character = Player.Character or nil
if character ~= nil then
positionPets(character, PlrFolder, dt)
end
end
end
end)
This script was found on YouTube to implement pet following, but it can only be visible on the client side. I want it to be implemented on the server side so that all players can see the effect of my pet following. How should I implement it

1 Like

try firing a remote event and pass through the player as a variable

My advise is to keep it client-sided. A pet follow system means a system that puts lots of parts (pets) into the right place every heartbeat or rendered frame of the game. At least, for a smooth experience. Doing this on the server could cause tremendous lag for players that are playing your game on a lower grade of devices.

Putting the pets into the right place on the client can still be percieved by all players, as long as they all have a script that moves pets. Instead of having a script on the client that only moves your own pets, give every client a script that moves the pets of every client to their user or whatever their user is targetting. This way if someone is playing on a bad device, you can add an option to disable the movement of pets of other players, reducing lag.

If executed on the customer service side, the server will not be visible

Yes, but the server doesn’t need to see the pets, only the players need to see them. Unless you have plans for the server to know where the pets are but I can’t think of any reason the server needs to know.

I want my pet to be visible to other players

Yes, that’s what I say. If you update the pets on every client for every player, it will still show all the players all pets but it won’t be as much of a load on the server.