How would i make a sort of table/function?

Hey i want to make a table that when called on picks 1 random thing out of all the things in the table and prints it.

So far iv tried watching youtube videos and looking at other posts without success. Iv even asked my friend and they dont know.

Technically you wouldn’t be writing the entire script if you did what i said since all your doing is printing it not coding it to actually be part of anything. Honestly though i dont really know if that counts so if it does then explain each service and etc since i know like 2 services.

use math.random()

Example:

local map = {cafe, work, home}

local random = math.random(1,3)

if random = 1 then
   load:FireServer(cafe)
elseif random = 2 then
   load:FireServer(work)
else
   load:FireServer(home)
end

What does this mean? Can you give an example of the table and what you want the results to be?

Here you go, added some notes so you could understand the code better. :)

local table = {"Apple", "Orange", "Banana"} --Table of items.

local function selectRandom() --Creates function.
	
	local rand = math.random(1,#table) --creates a variable that selects a number between 1 and how many items you have in your table. In this case, there's 3 items.
	
	if rand == 1 then --if the random number is 1, then it prints the first item in the table.
		print(table[1])
	elseif rand == 2 then --if the number is 2, then it prints the second item in the table.
		print(table[2])
	elseif rand == 3 then --if the number is 3, then it prints the third item in the table.
		print(table[3])
	end
	
end

selectRandom() --calls upon the function, therefore activating it.

You can do what @OverDayLight did but there’s a more efficient way:

local t = {'test1','test2','test3'}
local randomSelection = t[math.random(1, #t)] 
print(randomSelection) --it will print test1, test2, or test3
1 Like