Find a part by its index

Im trying to make a camera system that counts up in the folder for all the cameras, but i dont want to randomly choose a camera, i want to count up.

For Example:
Camera_Hall 1st in folder
Camera_Door 2st in folder
Camera_Room 3st in folder

Then count up from 1st to 2st to 3st then back to 1st.

2 Likes
for i=1, 3 do 
      local FolderNumber=i -- this is your folder number
      -- do whatever
end

are you trying to achieve something like this?
this will get your Folder number from 1 to 3, in the correct order

for i = 1, #FolderDirectory:GetChildren() do --> Change FolderDirectory to where your cameras are stored.
      print(i) --> We can print i which is the number of items in your FolderDirectory.
      --> The print should return the amount of Instances inside the folder.
end

no this is just counting not counting up instances in the folder

are you trying to loop through the instances in the folder? or are you trying to get the instances in the folder in a numbered sequence like 1, 2, 3, 4…?

yes im trying to loop trough the instances but not in a random order

same thing just counts the instances

thats what my script does.
you can name the instances in a numbered manner.

for example, name them 1, then 2, then 3 etc.

local FolderWithInstances= -- the folder which has the instances in numbered manner
for i=1, #FolderWithInstances:GetChildren() do 
      local FolderNumber=i -- this is your folder number
      local Object=FolderWithInstances:FindFirstChild(FolderNumber)
      -- this object is your instances in the numbered order"
end

i get error: attempt to get length of a Instance value

i edited the code, im sorry for the error last time.
test it again

nvm i forgot :getchildren() [maxchar]

im printing the object and it just says nil

are the instances named 1 2 3 etc?

no they are not named numbers [maxchar again]

what are they named? in order for us to easily access them, we need a numbering at the end of their name atleast

these are mostly still test names:
image

for i,v in pairs(FolderDirectory :GetChildren()) do --> Gets the children of FolderDirectory.
	print(i,v) --> Prints the index, and value of the children in the FolderDirectory.
end

can you name them Tripod_Camera1
Spawn_Camera2 etc?

here 1 will be the first instance and 2 will be the second

i believe the @123wolfyking needs instances in a sorted order, looping through the folder will return them in a random order

You’re going to have to order the camera parts manually. If you want to cycle, you can do something like this:

--!strict

local CameraParts: {BasePart} = {
	[1] = workspace.Camera1,
	[2] = workspace.Camera2,
	[3] = workspace.Camera3
}

local currentCameraPart: BasePart? = nil

local function cycle(direction: "next" | "previous"): BasePart
	local currentCameraPartIndex: number? = table.find(CameraParts, currentCameraPart :: BasePart)
	
	if currentCameraPartIndex ~= nil then
		if (direction == "next" and currentCameraPartIndex == #CameraParts) or (direction == "previous" and currentCameraPartIndex == 1) then
			return CameraParts[if direction == "next" then 1 else #CameraParts]
		else
			return CameraParts[currentCameraPartIndex + (if direction == "next" then 1 else -1)]
		end
	else
		return CameraParts[1]
	end
end

-- example cycle
local previousButton = script.Parent.Previous
local nextButton = script.Parent.Next

previousButton.Activated:Connect(function(): ()
	currentCameraPart = cycle("previous")
	-- do camera stuff
end)

nextButton.Activated:Connect(function(): ()
	currentCameraPart = cycle("next")
	-- do camera stuff
end)