Having multiple 'pets' at once?

function renderDuck(ducks)
	for _, v in pairs(ducks) do
		local PlayersDuck = Ducks['Tier ' .. v.Tier][v.Rarity]:Clone()
		PlayersDuck.Parent = Character
		
		local Duck = PlayersDuck.Duck
		
		local bp = Instance.new('BodyPosition')
		local bg = Instance.new('BodyGyro')
				
		bg.D = 750
		bg.MaxTorque = Vector3.new(25000, 25000, 25000)
		bg.P = 5000
		
		bp.D = 400
		bp.MaxForce = Vector3.new(1250, 10000, 1250)
		bp.P = 2000
					
		bg.Parent = Duck
		bp.Parent = Duck
	
		Duck.Position = HumanoidRootPart.Position + Vector3.new(0, 10, 0)
		
		spawn(function()
			RunService.RenderStepped:Connect(function()		
				bg.CFrame = HumanoidRootPart.CFrame
				bp.Position = CFrame.new(HumanoidRootPart.CFrame * Vector3.new(0, 2, 4)).p			
				wait()
			end)
		end)
	end
end

The problem I am having is it is creating all of the ducks inside of each other. So I want to know if there’s a way to spread them randomly, like how other games utilize pets


This is a bit of an exageration, but how there can be a lot of pets around the player

While you can see 3 ducks selected in the workspace, they all just fit into 1 spot

4 Likes

I wouldn’t do that in my opinion, that amount of pets will lag the server.

1 Like

I only want 3…

1 Like

Oh sorry, thought you wanted “infinite” pets

2 Likes

I think what the other game is referring to is an infinite number of pets to collect, not equip otherwise a player would spam equip them and make everything super unpleasant.

I’m not the best at this sort of stuff so if anyone has anything to add to this code, feel free I’d love to learn more too!

local Amount = 3
local Positions = {
[1] = "0, 0, 10", -- i'm not 100% sure on how you should properly save these numbers with string or without
[2] = "0, 0, 0",
[3] = "0, 0, -10",
}

for index, v in next, EquippedPets:GetChildren() do
if Amount >= 0 then
Amount = Amount - 1
local pet = v:Clone()
pet.Parent = workspace
pet.Position = Vector3.new(tonumber(Positions[index])) -- I'm not on studio so I'm not sure if this is how you will reference this position
end
else
print('Max pets')
end

I haven’t tested this out so let me know, this is just a basic draft that I made on the top of my head. Apologies if it isn’t what you wanted.

2 Likes

I used https://en.wikipedia.org/wiki/Unit_circle to create a circle around the character. My implementation looked something like this

local function GetPointOnCircle(CircleRadius, Degrees)
	return Vector3.new(math.cos(math.rad(Degrees)) * CircleRadius, 0, math.sin(math.rad(Degrees)) * CircleRadius)
end

From there I got the number of pets, and did 360/NoOfPets, then simply moved the pet to

-- Pos is the GetPointOnCircle
BodyPos.Position = (HumanoidRootPart.CFrame * CFrame.new(Pos)).p - Vector3.new(0, HumanoidRootPart.Size.Y / 2, 0) + Vector3.new(0, PetHead.Size.Y, 0)

It ended up looking like this

If you have any questions, feel free to ask me!

13 Likes

What is Pos in

CFrame.new(Pos)).p

1 Like

Pos is what I got from GetPointOnCircle(5, 360/NoOfPets)

1 Like
spawn(function()
	RunService.RenderStepped:Connect(function()		
		bg.CFrame = HumanoidRootPart.CFrame
		bp.Position = (HumanoidRootPart.CFrame * CFrame.new(GetPointOnCircle(5, 360/TotalPets))).p - Vector3.new(0, HumanoidRootPart.Size.Y / 2, 0) + Vector3.new(0, Duck.Size.Y, 0)		
		wait()
	end)
end)


They are all still inside each other. TotalPets is at 3

1 Like

I did update my GetPointOnCircle function, check you have the right one


Updated it, this is now the result. 2 of them are inside each other and the 3rd is off somewhere random? Idk where, but thats why the box is so large

1 Like

Make sure that when you do 360/NoOfPets you multiply it by the ith pet that you have equipped, so that with 3 pets it will be 120, 240, and 0, instead of all at 120

2 Likes

GetPointOnCircle(5, 360/TotalPets)

is what I have, with TotalPets == 3.

1 Like

Isn’t a BodyPosition a BodyMover?

2 Likes

oh im an idiot, didn’t realise his variable meant bodyposition/bodygyro

1 Like

When you loop through the code to change the position of each pet (supposedly) every frame, you’ll want to multiply 360/TotalPets by whatever no of pet that is, here’s how I did it

local RotationOffset = 360/#Pets

for i = 1, #Pets do
	local Pet = Pets[i]
	
	FollowScript(Pet, GetPointOnCircle(5, RotationOffset*i))
end

Note that follow script just does this

BodyPos.Position = (HumanoidRootPart.CFrame * CFrame.new(Pos)).p - Vector3.new(0, HumanoidRootPart.Size.Y / 2, 0) + Vector3.new(0, Head.Size.Y, 0)

where pet is the first parameter, and the second parameter is the offset I calculated from Unit Circle.

4 Likes

Kinda not sure how to properly implement that, ended up screwing with my script

function follow(Pet, Pos, bg, bp)
	spawn(function()
		RunService.RenderStepped:Connect(function()		
			bg.CFrame = HumanoidRootPart.CFrame
			bp.Position = (HumanoidRootPart.CFrame * CFrame.new(Pos)).p - Vector3.new(0, HumanoidRootPart.Size.Y / 2, 0) + Vector3.new(0, Pet.Size.Y, 0)
			wait()
		end)
	end)
end

function renderDuck(ducks)
	for _, v in pairs(ducks) do
		local PlayersDuck = Ducks['Tier ' .. v.Tier][v.Rarity]:Clone()
		PlayersDuck.Name = v.Tier .. ' - ' .. v.Rarity
		PlayersDuck.Parent = Character
		
		local Duck = PlayersDuck.Duck
		
		local bp = Instance.new('BodyPosition')
		local bg = Instance.new('BodyGyro')
				
		bg.D = 750
		bg.MaxTorque = Vector3.new(25000, 25000, 25000)
		bg.P = 5000
		
		bp.D = 400
		bp.MaxForce = Vector3.new(1250, 10000, 1250)
		bp.P = 2000
					
		bg.Parent = Duck
		bp.Parent = Duck
	
		Duck.Position = HumanoidRootPart.Position + Vector3.new(0, 10, 0)
		
		TotalPets = #ducks
		local RotationOffset = 360/TotalPets
		
		for i = 1, #TotalPets do -- ERROR HERE
			local Pet = TotalPets[i]
			
			follow(Pet, GetPointOnCircle(5, RotationOffset*i), bg, bp)
		end
	end
end

[ attempt to get length of upvalue ‘TotalPets’ (a number value)]

1 Like

Change the error line to

for i = 1, TotalPets do
1 Like

[ attempt to index upvalue ‘TotalPets’ (a number value)]

for i = 1, TotalPets do 
			local Pet = TotalPets[i]-- ERROR HERE
			
			follow(Pet, GetPointOnCircle(5, RotationOffset*i), bg, bp)
		end
1 Like

You’re attempting to get the index of the number of totalpets. You should change TotalPets[i] to where your pets are stored.

what you’re doing:

local Index = 1
local Pet = TotalPets[Index] -- yeah lol

what you should be doing:

local Index = 1
local Pet = AllPetsFolder[Index]
1 Like