Help with calling a function from a module script

Im trying to create a clone of the popular game Horrific Housing. I’m not doing this to copy the game. I’m not even gonna make it public. It’s just for learning purposes.

I’m using a module to compile all of the random events. Im having issues selecting the event function, and calling it.

local events = require(script.Events)

while true do
	wait(5)
	
	local chooseEvent = events[math.random(1, #events)]
	chooseEvent(game.Workspace.Baseplate)
end

This is some unfinished debug code I wrote just to test out the event system. all this does is choose a random function from the module, and call it. Well, that’s what its supposed to do.

Module:

local events = {}

function events.LavaPlatform(plate)
	plate.Touched:Connect(function(hit)
		local hum = hit.Parent:findFirstChild("Humanoid")
		
		if hum then
			hum.Health = 0
		end
	end)
end

return events

Edit: I forgot to say what the issue was. Here’s the error:

Screenshot_313

2 Likes

Require a module return a… dictionary if i’m correct.
So you can’t use a key of number to index it
You can use strings to find it
Example.
local ChooseEvent = events[“LavaPlataform”]

or do this

local events = require(script.Events)
local HoldingFunctions = {}

local i = 1
for Name, Function in pairs(Events) do
HoldingFunctions[i] = Function
end

local ChooseEvent = HoldingFunctions[math.random(1, i or #HoldingFunction)]
Note: You can’t use #Event to know the size of a dictionary

2 Likes

Thanks, works perfectly. I didn’t know that modules returned a dictionary and not a table. Thanks for teaching me something lol

1 Like

sorry, i forgot say: add a increment, i += 1 and you can save the functions as value
so HoldingFunctions[i] = Function
and use math random to index it

1 Like