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

How do I get the position of a table?

Now, I'll explain here. A while ago, I figured out how to make it so that it chooses a random item inside a table, but I want to know what place it is in. Still confused? Here's some code that I made.

local testing = {"Hello world!", "Hi!", "Greetings!"}
local r = testing[math.random(1, #testing)]

print(r)

Now say this would pick the second item in the table ("Hi!"). Now it would print what the string says, but I want it to print on what place the table is on. What I mean by that is in that example, it is on the 2nd item in the table. Basically, I want to figure out how to get the place the random item picker is on.

1 answer

✓ Accepted answer

Just make a variable:

local index = math.random(1,#testing)

And replace the variable local r with:

local r = testing[index]

The location of the value is what we call the index, and you can define it with one variable.

Your final code is this:

local testing = {"Hello world!", "Hi!", "Greetings!"}
local index = math.random(1,#testing)
local r = testing[index]

print(r)
print(index)

3 comments

0

thanks!

0

np

0

Or: table.find(testing, r)

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