You should use your for loop, but sort the table by whatever criteria you want. For example, by Y position:
local lights = something:GetChildren()
local function SortFunc(a, b)
return a.Position.Y < b.Position.Y -- your sorting criteria
end
table.sort(lights, SortFunc)
-- now do your for loop
I tested this on another place, it worked. But when I test it in the main game, it will not work, I have tried using the table, etc.
Code;
task.wait(1)
local beams = {}
for i,v in pairs(game.Workspace:WaitForChild("GroundSpotLights"):GetDescendants()) do
if v:IsA("Beam") then
table.insert(beams, v)
end
end
local function sort(a, b)
return a.Position.X < b.Position.X
end
table.sort(beams, sort)
local function tween(object, goals, timer)
local tween = game:GetService("TweenService"):Create(object, TweenInfo.new(timer, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0), goals)
tween:Play()
tween.Completed:Wait()
tween:Destroy()
end
script.Parent.MouseClick:Connect(function(plr)
if plr.UserId == 2407693045 then
for _, l in pairs(beams) do
pcall(function()
if l:IsA("Beam") then
l.Enabled = true
task.wait(.001)
end
end)
end
tween(game.Workspace:WaitForChild("Lighting"), {Color = Color3.fromRGB(175, 175, 175)}, .25)
task.wait(.025)
tween(game.Workspace:WaitForChild("Lighting"), {Color = Color3.new(0,0,0)}, .25)
else
plr:Kick("DENIED")
end
end)
task.wait(1)
local beams = {}
for i,v in pairs(game.Workspace:WaitForChild("GroundSpotLights"):GetDescendants()) do
if v:IsA("Beam") then
table.insert(beams, v)
end
end
local function sort(a, b)
return a.Position.X < b.Position.X
end
table.sort(beams, sort)
local function tween(object, goals, timer)
local tween = game:GetService("TweenService"):Create(object, TweenInfo.new(timer, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0), goals)
tween:Play()
tween.Completed:Wait()
tween:Destroy()
end
script.Parent.MouseClick:Connect(function(plr)
if plr.UserId == 2407693045 then
for _, l in ipairs(game.Workspace:WaitForChild("GroundSpotLights"):GetDescendants()) do
pcall(function()
if l:IsA("Beam") then
l.Enabled = true
task.wait(.001)
end
end)
end
tween(game.Workspace:WaitForChild("Lighting"), {Color = Color3.fromRGB(175, 175, 175)}, .25)
task.wait(.025)
tween(game.Workspace:WaitForChild("Lighting"), {Color = Color3.new(0,0,0)}, .25)
else
plr:Kick("DENIED")
end
end)
Make a Childfolder for each wave/line of light you want tweened. Name each Childfolder in order, from 1 to how many lines/waves of lights you have.
Put one or multiple lights into these Childfolders.
and then you just loop through it all!
local LightsFolder = workspace:WaitForChild("LightsFolder")
for Count = 1, #LightsFolder:GetChildren() do
local ChildFolder = LightsFolder:FindFirstChild(Count)
if ChildFolder then
for _, Light in pairs(ChildFolder:GetChildren()) do
if Light:IsA("Pointlight") or Light:IsA("SurfaceLight") or Light IsA("Spotlight") then
-- TWEEN STUFF for each light!
end
end
end
end
and if you want the loop to go backwards, just do this
for Count = #LightsFolder:GetChildren(), 1, -1 do
local ChildFolder = LightsFolder:FindFirstChild(Count)
if ChildFolder then
for _, Light in pairs(ChildFolder:GetChildren()) do
if Light:IsA("Pointlight") or Light:IsA("SurfaceLight") or Light IsA("Spotlight") then
-- TWEEN STUFF for each light!
end
end
end
end
Personally I use this method a lot, if I want to sort tweened stuff. I could go by sorting a table by any value like position.y, but with my method, you can go way beyond and make a huge neon palm tree, that will turn on/off the lights in the order you want them to.