math.random can be used for many things in Lua. For example, finding a random option in a table, or any random number between 2 numbers. To find a random number between 2 other digits, type math.random(lower, upper). Replace "lower" with the lower number, and upper with the greater value/number. table.getn CAN also be used in here. But it's place in math.random all depends on the length of the specified table.
Only problem is if you just type:
VAR = math.random(1,19)
And not put it under a repeat/while loop, it will always be equal to the same value. If you want it to be a different random number each time, you can do it any of these 2 ways:
1: repeat
R_Value = math.random(0, 17)
2: while true do -- true can be replaced with ANY running/true boolean.
R_Val = math.random(0, 17)
If you're doing this whilst modding Bully, you should also include the other stuff below it like:
Wait(0)
and an end or with a repeat loop use an until.
Either way, the variables will still work the same way, and equal a different number everytime it's used. I also like to use this for grapples, along with a grapple table, then just make it so it finds a random line on the table, and perform that node hehe.
Here is an example of it in use:
local Table = {}
function MissionSetup()
AreaTransitionXYZ(0,270,-110,7)
PlayerSetHealth(500)
end
function MissionCleanup()
gMissionRunning = false
WeatherRelease()
PlayerSetControl(1)
EnablePOI()
collectgarbage()
end
function main()
gMissionRunning = true
T_Setup() -- function to set up our table
while gMissionRunning do
local R = math.random(0,table.getn(Table))
if IsButtonBeingPressed(3,0) then
TextPrintString(Table[R].Text, Table[R].Time, 1)
end
Wait(0)
end
end
function T_Setup()
table.insert(Table,{Text = "yo dawg", Time = 2})
table.insert(Table,{Text = "Hello", Time = 3})
table.insert(Table,{Text = "u wot m8", Time = 4})
end
I do prefer to use while loops, rather than repeat, but repeat does work too:
local Table = {}
function MissionSetup()
AreaTransitionXYZ(0,270,-110,7)
PlayerSetHealth(500)
end
function MissionCleanup()
gMissionRunning = false
WeatherRelease()
PlayerSetControl(1)
EnablePOI()
collectgarbage()
end
function main()
gMissionRunning = true
T_Setup() -- function to set up our table
repeat
local R = math.random(0,table.getn(Table))
if IsButtonBeingPressed(3,0) then
TextPrintString(Table[R].Text, Table[R].Time, 1)
end
Wait(0)
until not gMissionRunning
end
function T_Setup()
table.insert(Table,{Text = "yo dawg", Time = 2})
table.insert(Table,{Text = "Hello", Time = 3})
table.insert(Table,{Text = "u wot m8", Time = 4})
end
The texts I inserted are completely random.
But I am pretty sure these 2 examples will actually work the way they are setup.
Also, in the repeat loop, you can also do:
until gMissionRunning ~= true
That will just repeat, until gMissionRunning equals ANYTHING other than true. Not just false.
I apologize if this is not very informative, but if you have any questions about this, please reply explaining what you don't understand or just generally have questions.
Also, on another note, NEVER name your table with the name "table". If you do, put a capital "T", UNLESS you are 100% sure you know exactly what you're doing.
Time also is a reserved function. if you just type "time" it may be another color, which means it was reserved for something else. Don't put a small "T" unless you are also sure you are using it right, since Lua is case sensitive.