Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.