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:
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. 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!
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.
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? I think I will then be able to integrate it into my system.
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
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