So I am currently making a game with a lot of randomizing and its a pretty basic game. I have all the gears scripted and everything but I just need to learn one thing. How do you do math.random with just not numbers? I know how it would usually work
--I know how to use it regularly local e = math.random(1,2) if e==1 then end else end
But I don't know how to do it with tables and more than 2 things
--Is this right? local e = math.random(#Lighting, #Lighting) if e==item1 then end elseif e==item2 then end else end
math.random() is a "function [that] returns a random integer between m and n. If only the first argument, m is passed, it returns an integer between 1 and m. However, if no arguments are passed, this function returns a random number between 0 and 1." -RobloxWiki
Math.random() will return a random integer between the two numbers (or arguments) that you give it. It cannot randomly pick between two objects, strings, booleans, etc. This function will always return a number (meaning it will always give you a number "back" when you call it).
If you have a table, you can us the #
constructor to get the length of that table as a number. If you feed this into math.random(), it will work fine because it is a number.
local myTable = {"Hi","my","name","is","Perci1"} local random = math.random(1, #myTable) --The first argument is not needed in this case, just used for clarity. print(random) --Print the random number print(myTable[random]) --Print the value of the table which is at the position of that random number
If you have any more questions feel free to ask.
You cannot use math.random on non-number datatypes. You can get a random item from a table like this:
tableofitems = game.Lighting:GetChildren() --I'm assuming you want to choose something from Lighting. local e = tableofitems[math.random(1, #tableofitems)] if e == item1 then print("e is item1!") elseif e == item2 then print("e is item2!") else print("e is not item1 or item2!") end