Help needed on ModuleScripts

So I have a function that will use tweenservice on a modulescript

This is what I usually do

local module = {}

function module.FunctionName()
	local ts = game:GetService("TweenService")
        local module = require(modulename)
end

return module

The thing I want to know is should calling services and requiring functions be in the function or someplace else? And if it should be someplace else where would it be?

If you’re requiring modules and getting services in a function, that means that every time you call that function, then it’s gonna require those modules and get those services everytime.

It would be more performant to put it outside of a function to only require those modules and get those services that way they only get required once (since you don’t need to get them again)

I would say that you should put these near the top of your script so it’s easy to see what things youve already defined. That way you don’t have to look for them

put those outside the function so it doesn’t get the service and require it again every time the function is called

@Doomcolp
@1kaelen1
But how do I put it out of the function?

local module = {}

local module = require(modulename)
local ts = game:GetService("TweenService")

function module.FunctionName()
	
end

return module

this way does not work

?

local module = {}

local modulename = require(modulename)
local ts = game:GetService("TweenService")

function module.FunctionName()

end

return module

Because your definition of module is being replaced / overwritten by the new definition for module

look:

local module = {}

is being changed to

local module = require(modulename)

since the variables are the same name

And is when you try to define a function, it doesnt define it under module = {} but instead it’s defined under the module you’re requiring module = require(modulename)

i am so confused the last 500 times that caused an error, now it is working though

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.