Loop though all parts inside a folder and get the cframe of them, one by one

Howdy, I want to get all the parts Cframes inside a folder, then move the camera Cframes, one by one.

like, the camera changes cframe to one child, five sec after, the next one, and so on until theres no more left and then just go back to the normal camera

the issue is that the loop gets ALL the cframes at once making it move constanly to the side

i have tried to change the kind of loop, move it to the server, and nothing works

this following script is my current attemp, that basically is the same, is activated though a remote event, but it gets all variables itself, is a local script, inside the Gui


local Cameras = workspace:WaitForChild("Cameras")


CameraEvent.OnClientEvent:Connect(function()
	Camera.CameraType = Enum.CameraType.Scriptable

	local children = Cameras:GetChildren()

	for i = 1, #children do
		if  children[i]:IsA("BasePart") then
			print( children[i].Name .. " CFrame:"..  children[i].CFrame)
			
			Camera.CFrame =  children[i].CFrame
			task.wait(5)

			children[i]:Destroy()
		end
	end
	
	

	Camera.CameraType = Enum.CameraType.Custom
end)

i also tried a inpairs loop, a while loop, and others, if any of you need more information, just ask!, id try my best to reply

Why making life harder and worse…


for i,v in Cameras:GetChildren() do
if  v:IsA("BasePart") then
			print(v.Name, v.CFrame)
			
			Camera.CFrame =  v.CFrame
			task.wait(5)

			v:Destroy()
		end
end

As far as I can tell the code does what you say it should do. I don’t understand what you mean by how it’s behaving.

Some things to watch out for.
Having that event fire multiple times while it’s running
Ordering incorrectness by just pulling children.
Another script moving the camera.

1 Like

what are you trying to say by that snippet of code?
I dont understand.

Nevermind, I thought you were talkiing about something else.

1 Like

Yet, as i said so, it prints and gives all the cframes, not just one

Server:
ServerScreenShot

Client:
imagen_2025-06-18_193912986

i changed the script in another attemp to fix it, now the server is the one giving the cframe to the client, but is basically the same, but at least is not moving weirdly!

Only issue I’m noticing here is that the yield is inside the statement that checks if the child is a BasePart. Are you sure that they are?

well, it is printing the cframe and the name on the server, so it haves to be or else it wont even print it

1 Like

Since this seems like it’s only intended to run once, you could simply set up CameraEvent.OnClientEvent to run once and run the code.

Workspace:WaitForChild("Cameras")
local CameraObjects = Workspace.Cameras:GetChildren()
for i = #CameraObjects, 1, -1 do
	local cam = CameraObjects[i]
	if cam:IsA("BasePart") == false then
		table.remove(CameraObjects, i)
	end
end

CameraEvent.OnClientEvent:Wait()
Camera.CameraType = Enum.CameraType.Scriptable

for _, cam in ipairs(CameraObjects) do
	Camera.CameraSubject = cam
	Camera.CFrame = cam.CFrame
	wait(5)
	cam:Destroy()
end

Camera.CameraSubject = Player.Character.Humanoid
Camera.CameraType = Enum.CameraType.Custom

actually now that i think about it, the print statement IS the problem, you can’t concatenate a CFrame with a string. (And I doubt you’d want to, CFrames are absurdly long. I suggest using position instead.)

Use string interpolation instead, or alternatively convert the CFrame to a string with tostring().

local cameras = workspace:WaitForChild("Cameras")

local camera = workspace.CurrentCamera
local cameraEvent = game.ReplicatedStorage.RemoteEvent

cameraEvent.OnClientEvent:Connect(function()
	camera.CameraType = Enum.CameraType.Scriptable

	for _, cameraPart in cameras:GetChildren() do
		if cameraPart:IsA("BasePart") then
			print(`{cameraPart.Name} CFrame: {cameraPart.CFrame}`)

			camera.CFrame = cameraPart.CFrame
			task.wait(5)

			cameraPart:Destroy()
		end
	end

	camera.CameraType = Enum.CameraType.Custom
end)

Is not working either, none of your code worked, to this point i started to think is my fault that this is failing

so i tried to disable every single script of my game one by one, and yup, idk how or why, but now it works.

thx for the help tho

What errors are you getting in the output? The print statement was definitely the problem.

like, it didnt printed anything at all, and the camera wasnt working with your script, i really dont know what happens there-

1 Like

Oh, the issue with my script was me getting the children of the Cameras folder before the game loaded. It should work now.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.