function module.SetUp()
for _, Drop in pairs(Drops:GetChildren()) do
local CD = Instance.new("ClickDetector")
CD.MaxActivationDistance = 15
CD.Parent = Drop
CD.MouseClick:Connect(function(Player)
module.SendPet(Player, Drop)
end)
end
end
module.SetUp()
is this in a module script?
if so then you should call module.SetUp()
outside of the module
its in a module script schould module.SetUp() after return module
Either do what @EggLord123 said or just require the script during startup and that should run it i think
module script:
function module.SetUp()
--the code
end
return module
some script:
local module = require(--location of the module)
module.SetUp()
did you meant like this?
I am pretty sure you could move the module.Setup() function above the return module in the Module Script then when you require it that function runs
local module = require(function()
function module.SetUp()
for _, Drop in pairs(Drops:GetChildren()) do
local CD = Instance.new("ClickDetector")
CD.MaxActivationDistance = 15
CD.Parent = Drop
CD.MouseClick:Connect(function(Player)
module.SendPet(Player, Drop)
end)
end
end
end)
module.SetUp()
return module
like this?
I dont use module scripts much myself, but arent functions in modules supposed to be written like this:
module.Setup = function()
--the code
end
and then called in other scripts?
local SetupModule = require(module)
SetupModule.Setup()
(may have misread the post a little, i dunno)
It would be more like
module.Setup = function()
–do code
end
module.Setup()
return module
In another script do local Module = require(modulePath)
that is not how you use require()
the first parameter of require()
has to be the location of the module in explorer
Well, both work, but writing
function module.Setup()
...
end
Looks much cleaner and is, from my understanding, the “intended” way. Writing them as a table like that makes everything less readable in my opinion, and they don’t allow you to use OOP in the same way
-- OOP example
local module = {}
module.__index = module
function module.new() -- Static function
local self = {}
setmetatable(self, module)
return self
end
function module:Foo() -- Only works on an object created with this module's module.new()
...
end
return module
do i need to put my code somewhere there in?
Yeah, this is the way you should do it:
-- Module script
local module = {}
function module.SetUp()
... -- Your Setup code here
end
return module
-- Other script
local module = require(<Path to module script>)
module.SetUp() -- Call the set up
Alternatively, you could do this:
-- Module script alternative
local module = {}
local function SetUp() -- This can only be called from within the script
... -- Your SetUp code
end
SetUp() -- Call the local function, this will run as soon as the module is required
return module
-- Other script alternative
require(<Path to module>) -- This will run the SetUp function
The reason I made the function local
is because SetUp
is rarely used more than once per require()
, so making it local prevents it from being called. If this is not what you want then you should use the first version instead.