Implementing a 3x6-grid-of-gui-buttons minigame in my game

  1. What do you want to achieve?
    I wanted to implement a 3x6 gui button minigame where it uses 2 colors: red and green (red being bad and green being good). I wanted half of the grid to be red and the other green while the entire thing scrambled.

  2. What is the issue?
    I tried using math.random(1, 2) to randomize the two colors. But, turns out it requires luck to play the minigame because the whole grid can be red (making the game unfair) and the whole grid can be green (making it not challenging).

  3. What solutions have you tried so far?
    I have tried to search on google and the Devforum but no luck.

Here’s the code I currently have.

local function ChangeVideoLayout()
	local Switch
	
	for _, v in pairs(buttons) do --buttons is a table in the localScript
		Switch = math.random(1, 2)
		if Switch == 1 then
			v.BackgroundColor = BrickColor.new(0, 255, 0)
		elseif Switch == 2 then
			v.BackgroundColor = BrickColor.new(255, 0, 0)
		end
	end
end

Make all the buttons red, then get 3 random ones a make them green.

for _, v in pairs(buttons) do
	v.BackgroundColor = BrickColor.new(255, 0, 0)
end

--Keep track of which were chosen already so
--the same one isn't set chosen multiple times
local buttonsLeft = table.clone(buttons)
for i = 1, 3 do
    local index = math.random(1, #buttonsLeft)
    buttonsLeft[index].color = BrickColor.new(0, 255, 0)
    table.remove(buttonsLeft , index)
end

I’m sorry, I’m new to coding but what does “int index” mean in the second for loop? This is because it gave me errors.

I’m sorry that was a typo (something used in a different coding language), it’s corrected now.

Yeah, so it gave me an error for the index variable and the error read that the code is attempting to get the length of a instance value. How can I make it so that it’s an int value or something that make it work. Here is the table:

local Holder = script.Parent

local buttons = {
	Holder.Video1,
	Holder.Video2,
	Holder.Video3,
	Holder.Video4,
	Holder.Video5,
	Holder.Video6,
	Holder.Video7,
	Holder.Video8,
	Holder.Video9
}

Try setting buttonsLeft to this instead:

local buttonsLeft = table.clone(buttons)

1 Like

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