Any way to store code in an array?

Hello, I quickly wanted to know if there would be a possibility to store code in an array, as shown below :

local code = {
      {"code1",
      print("hello!")}
      {"code2",
      print("hi!")
      if bool == true then 
                print("Oh!")
      end}

}

Thanks.

I think you can store them as functions since arrays can store functions if that’s what you’re wanting

local functionArray = {
	function()
		print("Yo")
	end,
	function()
		print("Sup")
	end
}

functionArray[2]() --Prints Sup since it gets the 2nd function and calls it
4 Likes

You can store a function :sunglasses:

local dict = {
     t = function(x)
          print(x)
     end;
}

print(dict.t(2)) -- prints 2
3 Likes

even better put it in a module script :sunglasses:

local module = {}

function checkIfAmazing(plr)
 if plr:isSmart() then
  return true
 end
end



return module

another script

local module = require(script.parent.ModuleScript)

print(Module.checkIfAmazing(game.Players.EmbatTheHybrid))-- true
print(Module.checkIfAmazing(game.Players.Moonvane))-- true
print(Module.checkIfAmazing(game.Players.ParentProfanities))-- true

1 Like

Right.
Tried it before this and it’s actually a good solution, but thanks for your help anyway!

1 Like

I wanted a script based module script so that worked fine, thanks.

2 Likes

i dont think you can store code in arrays unless its a function

I am not a really good expert, but isn’t it possible to save a function? On the same script and cetera yes, but you can’t like “export” the script using remote event/functions so I am not so sure… I think module scripts are a bit better because the can be required from everywere, server and client side. But that’s only my opinion and question, that’s why I am asking. The same question for @embatTheHybrid too