How would I create a table and randomly each time select one from it

As the title says, Im trying out tables as I don’t really use them much but I am now. I tried looking on google and Youtube for support of ways I could do this but I cannot. This is what I just worked out but it clearly doesn’t work.

If you know a way I could do this or fix it so it matches up with my code, that would be wonderful!

local LoadingNames = {
        "Loading Systems",
        "Loading Workspace",
        "Loading Players",
        "Getting modules"
    }
    local TweenText = PlayerGui:WaitForChild("LoadingScreen").Background.Text1
    local Variable = LoadingNames[math.random(#LoadingNames)]
    for i = 11, 0, -1 do
        task.wait(0.3)
        TweenText.Text = Variable
        task.wait(0.3)
        TweenText.Text = Variable
    end

from a first glance -
change this to:

LoadingNames[math.random(1,#LoadingNames)]```
#yourtablehere

returns the size of the table in total indexes.

You’d run a function like

math.random(1,#yourtablehere)

(Make sure to round for integers)

I’ve put that and kept out taking it out to see what would occur but it still just returns one value

So Im suppose to round for each “Word” Im guessing or no? (e.i, local Table = {1, 2, 3,4}

The random math should be rounded so you get your index number as an integer!

You are picking Variable and then running the loop, Variable is outside of the loop and so it never changes. Switch these two lines around. You need to run this top line again whenever you want to pick a new random table entry.

It doesn’t make a difference, math.random substitutes 1 as the first argument when given only one argument.

math.random only returns integers unless you run it with no arguments, so no need to do that.

2 Likes