Is there an easier way to use math.random for random words?

How do I make a script that is easier and faster to type than this

Use a table:

local randomWords = {
   "Word 1",
   "Word 2",
   "Word 3",
}

local chosenWord = randomWords[math.random(#randomWords)]
script.Parent.Text = chosenWord
2 Likes
local ItemTable = {
	
	["1"] = "Apple";
	["2"] = "Shirt";
	["3"] = "Bone";
	
};

local RandomNumber = math.random(1,#ItemTable)
local RecievedObject = ItemTable[tostring(RandomNumber)]
script.Parent.Text = RecievedObject

This is another way to do but it’s more efficient to use the method mentioned above my post.

1 Like

Can I have a question? Why did you use stings as index? Isn’t is simpler to have them as numbers and avoid using tostring? Thank you :smiley:

Correct me if i’m wrong but i’m not sure you can set values to integers.

Screen Shot 2020-12-25 at 17.55.14

1 Like

You can you have to either surround the numbers in brackets or completely omit them for an array. These two tables are the exact same

local firstTable = {
    [1] = "First word",
    [2] = "Second word"
}
local secondTable = {
    "First word",
    "Second word"
}
1 Like