Help with this script

Hi, devforum. I wanted to make one of a swarm of object(for example: pets)'s bodypositoin to be at a part when I click on the part. The problem is i’m stuck on the pets part. Idk how I make only one pet go to the part. :sob: :man_facepalming:

Here’s the code:

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	local hrp = plr.Character.HumanoidRootPart
	local pets = hrp:FindFirstChild("pet1","pet2","pet3","pet4","pet5")--this line of code is cringeeeeee
	pets.BodyPosition.Position = script.Parent.Position
end)
script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	local hrp = plr.Character.HumanoidRootPart
	for i,v in pairs(hrp:GetChildren()) do
        if v.Name:lower():find('pet') then
            v.BodyPosition.Position = script.Parent.Position
        end
    end
	pets.BodyPosition.Position = script.Parent.Position
end)

FindFirstChild takes one argument and returns one instance whereas :GetChildren returns all of the children of a specific instance.

1 Like

thanks. not really what i wanted but this works too

Wait, didn’t I said one pet, right? Is there a way to do that?

Oh, must have skipped over that part, it was 4 am when I wrote that haha.

Do you want to randomly choose one from the pets?

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	local hrp = plr.Character.HumanoidRootPart
	local pets = hrp:GetChildren()
    pets = pets[math.random(1, #pets)]
	pets.BodyPosition.Position = script.Parent.Position
end)

Yes. Thank you! Also, what were you doing 4 in the morning?

Ruining my sleep schedule :stuck_out_tongue:

1 Like

:flushed:

chaaaaaaaaaaaaaaaaars

1 Like