• math.random in a table?
    4 replies, posted
I have this table here local teams = {} --Citizen [CODE]teams[TEAM_CITIZEN] = { name = "Citizen", color = Color( 39, 174, 96 ), model = { "models/player/Group01/Male_01.mdl", "models/player/Group01/Male_02.mdl", "models/player/Group01/Male_03.mdl", "models/player/Group01/Male_04.mdl", "models/player/Group01/Male_05.mdl", "models/player/Group01/Male_06.mdl", "models/player/Group01/Male_07.mdl", "models/player/Group01/Male_08.mdl", "models/player/Group01/Male_09.mdl", }, weapons = { "keys", "weapon_physcannon", "weapon_physgun", "gmod_tool" } }[/CODE] How would I use this code here to randomly pick a model from the list? [CODE]function plyMeta:SetGamemodeModel() local teamN = self:Team() --[Set model] self:SetModel( "models/player/Group01/Male_09.mdl" )//teams[teamN].model ) for k, v in pairs( teams[teamN].model ) do self:ChatPrint( "Picked model: " .. v ) --debug, prints out 9 lines of all the models. How would i select one of them at random? end end[/CODE] Thanks!
[url]https://wiki.garrysmod.com/page/table/Random[/url]
-ninja'd-
You can just generate a random number, and use it as an index for the table. [CODE]local rnd = math.random(1,#teams[TEAM_CITIZEN]["model"]) self:SetModel( teams[TEAM_CITIZEN]["model"][rnd] )[/CODE] -ninjaed too-
Thanks guys, although I managed to figure it out on my own seconds after posting this. This is what I did [CODE]self:SetModel( teams[teamN].model[math.random( 1, table.Count( teams[teamN].model ) )] )[/CODE]
Sorry, you need to Log In to post a reply to this thread.