TheSalzu
(サルズ)
August 19, 2021, 2:32am
#1
Hey so I searched the web far and wide and couldn’t figure out how to do this. If you’re confused which you’re probably are let me show a example.
Example
As you can see they have a local/Server script which requires all the module scripts which run the code for the game. But I’m confused on how the run the code. They don’t call any functions or anything. Please and thank you.
When requiring a module, it causes all the code in the module to be ran, and it returns to the caller script whatever the module returned.
TheSalzu
(サルズ)
August 19, 2021, 2:35am
#3
Ah so if I had a module like this
local Module = {}
print'Heya'
return Module
This would print “Heya”?
But what if they we’re wrapped in a function to organize the code?
I’m not sure what you mean by this, can you show an example?
To answer your question, yes.
TheSalzu
(サルズ)
August 19, 2021, 2:38am
#5
local Module = {}
local function DoSomething(Arg)
print(Arg)
end
print'Something else'
return Module
A function to organize the code
You should do this:
local Module = {}
Module.PrintFunction = function (message)
return message
end
return module
If you want to call it just do:
print( require(game.ServerStorage.Module).PrintFunction("MyMessage")) -- change the game.ServerStorage.Module to your module's path
It will print Something else, but that it is it.
TheSalzu
(サルズ)
August 19, 2021, 2:40am
#8
But what I would want to do something like is.
-- In a local/Server script
for _,Module in pairs(script:GetChildren()) do
require(Module)
end
TheSalzu
(サルズ)
August 19, 2021, 2:42am
#9
Ah I see. I shall consult my friend for advice since he is a expert in this. I wanted to come here and practice since he was sleep. Thank you very much!!
Can you just say what do you want to do? Like give player coin or make player do thsis or that or what?