I want to make the individual lights flash in random order when I press a button. I already made a script that makes them flash in order, I just don’t quite know how to randomize it.
Here’s my current script for them:
game.Workspace["Reset Button"].ClickDetector.MouseClick:Connect(function()
script.Parent.Visible = true
script.Parent.Parent.SlidingFrame.Visible = false
while true do
for _, v in pairs(script.Parent:GetDescendants()) do
if v:IsA("Frame") then
v.BackgroundColor3 = Color3.fromRGB(200, 0, 255)
local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(
0.75,
Enum.EasingStyle.Exponential,
Enum.EasingDirection.Out,
0,
true,
0
)
local ColorChange1 = TweenService:Create(v, Info, {BackgroundTransparency = 0})
ColorChange1:Play()
wait(0.1)
end
end
end
end)
game.Workspace["Reset Button"].ClickDetector.MouseClick:Connect(function()
script.Parent.Visible = true
script.Parent.Parent.SlidingFrame.Visible = false
while true do
for _, v in pairs(script.Parent:GetDescendants()) do
if v:IsA("Frame") then
task.delay(math.random(0, 10)/10, function()
v.BackgroundColor3 = Color3.fromRGB(200, 0, 255)
local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(
0.75,
Enum.EasingStyle.Exponential,
Enum.EasingDirection.Out,
0,
true,
0
)
local ColorChange1 = TweenService:Create(v, Info, {BackgroundTransparency = 0})
ColorChange1:Play()
wait(0.1)
end)
end
end
end
end)
Your current script is running through the frames in the order they’re returned by the GetDescendants() function. In order to make the lights flash in a random order, you’ll need to shuffle this list.
Lua doesn’t have a built-in function for shuffling a table, but you can use the Fisher-Yates shuffle, also known as the Knuth shuffle, to do this. Here’s a function you can use:
function shuffle(tbl)
for i = #tbl, 2, -1 do
local j = math.random(i)
tbl[i], tbl[j] = tbl[j], tbl[i]
end
return tbl
end
You can call this function to shuffle your table of frames before you run through them:
script.Parent.Visible = true
script.Parent.Parent.SlidingFrame.Visible = false
while true do
local frames = script.Parent:GetDescendants()
shuffle(frames)
for _, v in pairs(frames) do
if v:IsA("Frame") then
v.BackgroundColor3 = Color3.fromRGB(200, 0, 255)
local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(
0.75,
Enum.EasingStyle.Exponential,
Enum.EasingDirection.Out,
0,
true,
0
)
local ColorChange1 = TweenService:Create(v, Info, {BackgroundTransparency = 0})
ColorChange1:Play()
wait(0.1)
end
end
end
This version of the script shuffles the order of the frames before it begins each cycle of flashing them. As a result, the frames will flash in a different random order each time.
You just need to put the shuffle function above your current function.
game.Workspace["Reset Button"].ClickDetector.MouseClick:Connect(function()
script.Parent.Visible = true
script.Parent.Parent.SlidingFrame.Visible = false
while true do
for _, v in pairs(script.Parent:GetDescendants()) do
if v:IsA("Frame") then
v.BackgroundColor3 = Color3.fromRGB(200, 0, 255)
local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(
0.75,
Enum.EasingStyle.Exponential,
Enum.EasingDirection.Out,
0,
true,
0
)
local ColorChange1 = TweenService:Create(v, Info, {BackgroundTransparency = 0})
local r = math.random(1, 10)
If r == 1 then
ColorChange1:Play()
end
wait(0.1)
end
end
end
end)