#How to select a random table within a module?

8 messages · Page 1 of 1 (latest)

odd knot
#

Hi, I'm making a module that contains tips for certain places. Ignore the places and tips, it's a meme game.

As I was assembling the main module I thought:
"How I can choose a random place between all of them in the tips table, and assign it to a text button in the GUI that lets you select the place?"

Here's what I have so far, it's not complete yet but I'd like a bit of help to finish it

local tips = {
    ["Arizona"] = {
        "Beware the sunstroke debuff. Try to alleviate it before your health reaches 0",
        "Flipflops are inconvenient to wear here, they will hinder your movement",
        "",
    }
}

function tipDisplay(name, place)
    local randomTip = tips[math.random(1, #tips[place])]
    local randomPlace = tips[math.random(1, #place)]
end

return tips

The image attached is the gui that lists the selectable places

any help is appreciated, thank you in advance

meager moth
#
local selectRandom = function()
  local num = select("#", table.unpack(tips))
  local r = select(math.random(1, num), table.unpack(tips))
  return r
end
frank gale
#
local Places = {Arizona,Australia,Bazil}
local Place = Places[math.random(1,#Places)]
-- #table = the number of how manny things are inside of a table
meager moth
#

i mean that works

#

sure

#

i wish lua had a function to get keys LOL

#

i guess you could make your own