How would I make lights in a set flash randomly?

Hello. I have an issue with a random light script. I made a sort of light out of a SurfaceGui and Frames for the individual lights. It looks like this

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)

And here’s what that does to the light:

https://i.gyazo.com/e2d014631976ba4764bdfc23c9837cfe.mp4

How would I randomize the order the lights flash in?

Thanks for any help :slight_smile:

1 Like

try this

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)
1 Like

Sorry for late reply, but that just crashes the game.

Any more help would be appreciated!

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.

1 Like

Try this

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)
1 Like

This works very well! Thank you for the help. :slightly_smiling_face:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.