How can I loop through a folder with Runservice instead of a for loop?

I got this code that uses a folder of CFrameValues to change the CFrame of a player’s camera.

The problem is when I need to loop through the folder, is there a way to use RunService instead of a for loop since RunService is a loop?

local folder = game.Workspace:WaitForChild("CutScenes"):WaitForChild("Camera1"):WaitForChild("Frames"):GetChildren()
local frames = #folder
local camera = workspace.CurrentCamera
local RunService = game:GetService("RunService")


game:GetService("RunService").RenderStepped:Connect(function()
	for i=1,frames do
		camera.CameraType = Enum.CameraType.Scriptable
		local cf = folder[i]
		camera.CFrame = cf.Value
	end
end)

Use a run service while loop to change the cframe to the index

local folder = game.Workspace:WaitForChild("CutScenes"):WaitForChild("Camera1"):WaitForChild("Frames"):GetChildren()
local frames = #folder
local camera = workspace.CurrentCamera
local RunService = game:GetService("RunService")

camera.CameraType = Enum.CameraType.Scriptable
local index = 0
local cf = nil
while game:GetService("RunService").RenderStepped:Wait() do
     index = index + 1
     if folder[index] == nil then break end
     cf = folder[index]
     camera.CFrame = cf.Value
end

2 Likes

This is making the camera go all funky


I think it might just being looping randomly
image
another edit: it appears I’m correct, I added a print and it seems to be going through random frames, how can I fix this?
image

Youll need to instead define a new array, The sort it according to your order, Which you can use table.sort, And also I suggest using beizier curve or something, idk as the script executes every 16 mili second and thus TP very fast

I’ve been trying forever to use moon animator as a way to make cutscenes easily
So I replace the folder with a table?

No I mean sorta like and you gotta also name them in order, Alphabet would be best

local folder = game.Workspace:WaitForChild("CutScenes"):WaitForChild("Camera1"):WaitForChild("Frames"):GetChildren()
folder = table.sort(folder, function(a,b)
	return a.Name< b.Name
end)

local camera = workspace.CurrentCamera
local RunService = game:GetService("RunService")

camera.CameraType = Enum.CameraType.Scriptable
local index = 0
local cf = nil
while game:GetService("RunService").RenderStepped:Wait() do
     index = index + 1
     if folder[index] == nil then break end
     cf = folder[index]
     camera.CFrame = cf.Value
end