Don't know how to select a random item from a table and store for later use

local colours = {
    blue,
    yellow,
    red,
    green
}

local chosenColour = math.random(1, #colours)

In my code I’m aiming to pick a random colour from the colours table (which I know how to do), but I want to be able to store the selected colour as a variable to be used throughout the round. The problem is, I want to store the colour to be used for only one round, and to pick a new random colour for every round, and I’m unsure how to do this. Any help? (sorry if I’m explaining this poorly, feel free to ask questions)

I’ve looked around but don’t seem to find any assistance for this.

show round script bro. plz just for me

Currently you are just storing a random number
Change it to


local chosenColour = colours[math.random(1, #colours)]

but assuming ur round script is like this

while true do
-- code
end

variables are re-definded inside each while loop(round). As long as you put the variable inside the while loop it’ll only be the color for just that round. so like this

while true do
local colours = {
    blue,
    yellow,
    red,
    green
}

local chosenColour = local chosenColour = colours[math.random(1, #colours)] -- this will be different per round
end
1 Like

Can I call the colour I get again in the loop? e.g I get blue and want to use blue for something else in the loop

yes, as long as the chosen colour variable is inside the parent loop.

Alright, thank you. Let me give this a shot :slight_smile:

It works! Thank you. Just a quick question so that I understand the code… why does it reset each round and how does chosenColour stay the same in the loop instead of giving me a different value each time the variable is used?