Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Incredibly easy question; how to print ONE thing out of a table?

Asked by
Qorm 100
9 years ago
random=game.Workspace.minigames.selection:getChildren()
    for i=1,#random do
        print(math.random(i))
        end

This prints EVERYTHING inside the table. I can't figure out how to only print ONE thing out of the table randomly.. It's extremely obvious but I can't find it anywhere and help would be appreciated... t-t

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

First, pick a better name:

local children = workspace.minigames.selection:GetChildren()
-- children. Not "random"

Where you are printing:

print( math.random(#children) )

This will print a number (not a thing in the table).

But you're doing that in a for loop:

for i = 1, #children do

So if you only want to do it once, just don't include the loop.


If you want one random element from a list, you can do this:

local randomIndex = math.random(#list)
-- a random INDEX of `list`

print( list[ randomIndex ] )
-- a random thing (random becaue it's at a random *index*) from list
Ad

Answer this question