Problem with going through Table/List of Parts

Introduction

Hello there,
I’m currently trying to make a flying camera intro, basically a Part that is flying around the map to various destinations. To make this, I put the player camera in a Camera Part, that I’m tweening around the map to other parts to create the scene.

The Problem

I got all the Parts in a Folder in the Workspace, numbered from 1-8 in this case. I then tried to tween the CameraPart to each part via for loop. A friend told me, that for loops always “run” through the Explorer in it’s listed sequence, so I numbered the parts from 1-8 as I said above. My problem is, that the CameraPart still tweens to random parts in the table of parts it should tween to, and not in the sequence I numbered them.

My Explorer:
image

The CameraPart should Tween to the position and orientation of each Part in the FlyToParts Folder.

My Code, LocalScript in StarterPlayerScripts:

local RS = game:GetService("RunService")
local TS = game:GetService("TweenService")

local CameraPart = workspace.CameraSystem.CameraPart
local FlyToParts = workspace.CameraSystem.FlyToParts
local Camera = workspace.Camera

local SceneRunning = false
local Tween = nil
local Goal = nil
local TweenTime = 2

Camera.CameraType = Enum.CameraType.Scriptable
Camera.CameraSubject = CameraPart
Camera.CFrame = CameraPart.CFrame

local TI = TweenInfo.new(TweenTime,Enum.EasingStyle.Linear)

RS.RenderStepped:Connect(function()
	if SceneRunning then
		Camera.CFrame = CameraPart.CFrame
	end
end)

function FlyScene()
SceneRunning = true
	for _, FlyToPart in pairs(FlyToParts:GetChildren())do
		Goal = {}
	Goal.Position = FlyToPart.Position
	Goal.Orientation = FlyToPart.Orientation
		Tween = TS:Create(CameraPart,TI,Goal)
		Tween:Play()
		wait(TweenTime)
	end
	print("Flight ended")
end

FlyScene()

The Result

robloxapp-20210605-1453112.wmv (2.0 MB) Sorry for the lag, I don’t have that good of a PC. :sweat_smile:
Also, no errors in the Output.

I’m not the best of a scripter, but I hope you can help me with my issue. Any help is appreciated! :slightly_smiling_face:

Get children cannot gurantee the order the parts are returned. You will need to sort their names in the table using table.sort then iterate through the sorted table numerically.

1 Like

Thanks for your help, I tried it and didn’t really understand how to use it. Could you give me a piece of code, so I can see how to use it correctly? :sweat_smile: I think I will then be able to integrate it into my system. :smile:

I believe you have to use ipairs() to get it in order for an array, though I may be wrong.

1 Like

What you can do is

local Table = {}
for i,v in pairs(CameraPartsFolder:GetChildren()) do --- gets all the children in FlyTo Parts
table.insert(Table, v) --- Puts all the parts in the table and i think since its already named in order it'll sort automatically
end

for i,v in pairs(Table) do -- now it should go through the list in order since its already named in order(12345 etc)
local Tween = game:GetService("TweenService"):Create(CameraPart, TweenInfo.new(TweenTime, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Position = v.Position, Orientation = v.Orientation})
Tween:Play()
Tween.Completed:Wait()
end
1 Like

I tried ipairs but it still didn’t work in that sequence. :frowning:

1 Like

Why don’t you print the “FlyToPart” in the for loop every time it iterates so we know what order it’s going in (might help)

1 Like

Doesn’t

CameraPartsFolder:GetChildren()

already put the CameraParts in a table? As @dthecoolest said, GetChildren() cannot guarantee the order of the parts. But I’ll try.

Good Idea, I’ll try! I think they’re selecting random though.

image
image
Yes, they’re not following the order, and never in the same order.

1 Like

You need to sort the children before using them

local children = CameraPartsFolder:GetChildren()
table.sort(children, function(a, b)
    return a.Name < b.Name
end)
1 Like

Try This maybe? Errrrrrrrrrrrrrrrr

1 Like

Ahh okay, I gonna try that!
30-------chars

Might also need tonumber() since you cannot compare strings only.

local cutsceneParts = FlyToParts:GetChildren()

table.sort(cutsceneParts,function(a, b)
    return tonumber(a.Name) < tonumber(b.Name) 
end
)
for _, FlyToPart in pairs(FlyToParts:GetChildren())do
    Goal = {}
Goal.Position = FlyToPart.Position
Goal.Orientation = FlyToPart.Orientation
    Tween = TS:Create(CameraPart,TI,Goal)
    Tween:Play()
    wait(TweenTime)
end

You can compare them, but it will be compared as strings, so “16” is greater than “150” due to the way strings are compared, but with them as a number, 16 is less than 150, so putting tonumber() would probably be best

1 Like