Button triggers random death sequences, how do i store the sequences?

Alright so im very new to scripting, ive been an avid full time builder for about 1 year and have been building on and off since 2017ish anyway wanted to try something alittle new, and delve into scripting.
This is what i consider my first real project since every thing i scripted came from my head.

Anyways more of a showcase, but i need help storing data. Basically i have a math.random(1,6) And have an else if x == (whatever random number) then (code a special event like nuke falling from sky, or get hit by bus) Anyway point is when i get to 20 different events storing that in 1 script isnt going to be the most efficient, so how else do i store it?

If you got a lot of something you put it in a table.

Here’s the formula for it you can find similar to a random map or minigame script in a tutorial somewhere:

local randomEventsTable = {} -- store functions doing an specific event here
local function magicEvent()
print("Magic!")
end
table.insert(magicEvent,randomEventsTable)

local chosen = randomEventsTable[math.random(1,#randomEventsTable)]
chosen()--execute the function

You can use a module to store the functions and chose one at random. Would be more organized.

And adding onto the first reply, you can do:

local randomEventsTable = {}

function randomEventsTable.Event()
   --event
end

--etc

I think you accidentally flipped the order of table.insert haha, the first argument is the table to insert something to and the 2nd argument is the thing to insert

So

table.insert(magicEvent,randomEventsTable)

Should be

table.insert(randomEventsTable, magicEvent)

But yea the best way in this case is to just use a table of functions to run, example

local events = {
	function()
		print("Woah!")
	end,
	function()
		print("Ah!")
	end,
	function()
		workspace.Baseplate:Destroy()
	end
}

local chosenEvent = events[math.random(#events)]
--Later on
chosenEvent()

--Or if you don't plan on firing it later

events[math.random(#events)]()

Or using other containers, such as ModuleScripts, which somewhat answers your question of

1 Like

thank you very much good people of the interwebs!

1 Like