Help with pet equip, when playing multiplayer

  1. What do you want to achieve? Keep it simple and clear!

i will like to have RenderStepped disconnect for each player if renderstepped is running for that player
so that i can see all pets

  1. What is the issue? Include screenshots / videos if possible!

i work fine when playing solo but when i try to join with my smuf and get some pets i can’t see my pets but can see my main pets and on my main acc can i only see my smuf pets?

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

i try to look on dev hub and fine out by my self but no luck

This is a local script

local RS = game:GetService("ReplicatedStorage")
local removeEv = RS:WaitForChild("Remote"):WaitForChild("PetsEquip")

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

local conn

function stuff()
	for i, player in pairs(game.Players:GetChildren()) do	
		print(player)
		local ts = game:GetService("TweenService")
		local stop = false
		local stop2 = false
		local PlayerEquip =	player.Character:WaitForChild(player.Name.." Pets")
		local hrp = player.character:WaitForChild("HumanoidRootPart")
		
		local pets = {}
		
		for _, pet in pairs(PlayerEquip:GetChildren()) do
			table.insert(pets, pet)
		end
		
		if conn then conn:Disconnect() print("stop") end
		conn = game:GetService("RunService").RenderStepped:Connect(function()
			for i, pet in pairs(pets) do
				local angle = i * (fullCircle / #pets)
				local x, z = getXAndZPositions(angle)

				local position = (hrp.CFrame * CFrame.new(x, 0, z)).p
				local lookAt = hrp.Position
				pet.PrimaryPart.CFrame = CFrame.new(position, lookAt)
			end
		end)
	end
end

removeEv.OnClientEvent:Connect(stuff)


1 Like

You immediately disconnect the connection from the previous player when you make a new one. This means that it constantly resets the connection and only works for the last player called. Here is how you should do it:

local RS = game:GetService("ReplicatedStorage")
local removeEv = RS:WaitForChild("Remote"):WaitForChild("PetsEquip")

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

local conns = {}

function stuff()
	for i, player in pairs(game.Players:GetChildren()) do	
		print(player)
		local ts = game:GetService("TweenService")
		local stop = false
		local stop2 = false
		local PlayerEquip =	player.Character:WaitForChild(player.Name.." Pets")
		local hrp = player.character:WaitForChild("HumanoidRootPart")
		
		local pets = {}
		
		for _, pet in pairs(PlayerEquip:GetChildren()) do
			table.insert(pets, pet)
		end
		
		if conns[player] then conns[player]:Disconnect() print("stop") end
		conns[player] = game:GetService("RunService").RenderStepped:Connect(function()
			for i, pet in pairs(pets) do
				local angle = i * (fullCircle / #pets)
				local x, z = getXAndZPositions(angle)

				local position = (hrp.CFrame * CFrame.new(x, 0, z)).p
				local lookAt = hrp.Position
				pet.PrimaryPart.CFrame = CFrame.new(position, lookAt)
			end
		end)
	end
end

removeEv.OnClientEvent:Connect(stuff)
1 Like