How to select a random table within a module?

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
For those curious, here's the GUI of the places selectable

any help is appreciated, thank you in advance

1 Like

You looking for something like this?

local module = {}
module.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"
	}
}
local rand = Random.new()


-- e.g. module.GetRandomTipFrom("Arizona")
function module.GetRandomTipFrom(place: string)
	local index = module.tips[place]
	
	if index then
		return index[rand:NextInteger(1, #index)]
	end
end

return module
1 Like

This is awesome.
I can just require the module from the textlabel and run the randomtip function there, right? Or how can I implement it cause I’m not very experienced with modules just yet

Bad attempt at code in the TextLabel
local tipsModule = require(game:GetService("ReplicatedStorage"):WaitForChild("TipsModule"))

tipsModule.GetRandomTipFrom()
1 Like

Yep! Just don’t forget to input a string inside the function (like for example, “Arizona”)

so, something like this should do the trick?

tipsModule.GetRandomTipFrom("Arizona")

And I guess I just make a loop that waits a couple of seconds and runs the function again?

1 Like

Yep, that should work.

[dont mind this]

alright, epic. Now I just need to figure out how to get the buttons to trigger the function to change the text on the select GUI

I appreciate the help man

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.