Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Trying to get random function from module and run it. Code is not working correctly, help please?

Asked by 2 years ago
Edited 2 years ago

I have a script that runs the functions every 1 second in server script service, heres the script

Added some comments to make it easier to understand

local functions = require(script.Functions) -- module

local MethodNames = {} --table to insert functions inside of

while true do 
    wait(1)
    for MethodName, Method in pairs(functions) do
        if type(Method) == "function" then --make sure that this value is a function
            table.insert(MethodNames, MethodName)
            local RandomMethodName = MethodNames[math.random(1, #MethodNames)] --getting the random function
            Method()
        end
    end
end

Then the module script which contains the functions

local Functions = {}

function Functions.Print()
    print("1st Event Triggered")
end

function Functions.PrintAgain()
    print("2nd Event Triggered")
end

return Functions

When I run the game it just runs both of the functions at the same time.

Answer this question