I know how to use module scripts and everything like that, same with OOP, but I don’t know when and why to use them. All my scripts just print values, how can I implement that into real game features? Also, I see people saying that you can make a function in a module script, and then use it in a server script or something like that, am I mistaken, or is that a real feature? I’d appreciate any help on this topic.
module:
return function(foo: string)
print(('%s kinda sus'):format(foo))
end
script / localscript:
local module_ = require(path.to.module)
module_('ROBLOX')
Imagine a cutscene that involve scripting the camera, normally you would just like use a tween effect on it and you will create a lot of tweens to tween the camera. This is insufficient and your code will look long and messy. Using a module script will make your code look cleaner.
ModuleScripts are cool because with them, you can utilize “dependencies” more in your programming, which in turn leads to a cleaner and more manageable work environment.
For example, say we have a weapon engine. That could be one ModuleScript. And let’s say that framework has dependencies on other ModuleScripts, but those ModuleScripts really would only impact the gun engine; no other systems in the game should need those. So, we “chunk” the work of the gun engine module by splitting it into numerous other modules to make it easier to manage our code. For example, it’d make sense to have separate modules for stuff such as weapon specs. That could definitely be utilized (or depended upon) by many other systems we have in the game, such as maybe a user interface that would display those weapon stats on a screen.
ModuleScripts can also be “required” (or loaded) both on the server or the client. Once required, it caches the value returned once to the script that calls it, and then returns the cached value to any other script that requires it. (So if you’re returning a table, for example, it’ll return the same table object and not a new object each time you require the module)
However, a module loaded server-sided can only be utilized on the server, and vice-versa if you load it locally. So basically each ModuleScript has two environments, client and server, and you can use it in both. For each environment it essentially gets its own copy of the ModuleScript’s contents.
Hopefully that helps a bit, apologies if it’s not efficiently worded
Yes it is.
local module = {}
module.Memes = function()
print("C_Sharper is cool")
end
module:More = function()
print("incapaz is cool")
end
We write the code only once in the module scripts and call it from multiple different scripts.
local module = require(game.ReplicatedStorage.module)
We use require function to return the date from the modulescript. Using this we can execute functions from the modulescript.
Module Scripts are not always placed in ReplicatedStorage.
ModuleScripts are placed in ServerScriptService when used by server-side Scripts and ReplicatedStorage when used by client-side LocalScripts.
This won’t work, since colon syntax can’t be used like that. x:y
should always be followed by a ()
, since it’s a way to call it that specifies a self parameter. You can fix it by changing it to module.More = function(self)
or just use the typical colon syntax and do function module:More()
. I’d personally recommend just ditching the self parameter, since you don’t use it, but it’s good to know how to define a function with it properly anyways.
I now know how to call functions from module scripts to scripts to make my work place easier to manage, but what do I put in the function in the module script? Like how can I make for example, a ragdoll engine. Do I just do normal functions that a slightly more detailed and flexible, or are there like complicated functions?
function module:More()
-- your meme functions here
end
I have to go soon, so I will have to read over everything tomorrow. You can respond and say stuff if your time zone is different, I can read it when I wake up.
EDIT: I looked around a bit, and I figured out most of what I wanted to do for the day, I will check what you guys added tomorrow.
With this, I can use the global self
word in the scope?
Doing a:b() will let you use self within the function.
Here’s an example of using a module script:
local module = {} -- create a table to store stuff such as functions
module.testing = 'This is a test'
function module.isCool(argument)
print(argument, 'is cool!')
end
function module.myFunc(argument)
print('function test |', argument)
end
function module:example()
print(self.testing)
end
return module -- once we require our module in a script the module table will be returned
The in our other script we can do this:
local module = require(path.to.module)
module.isCool('Roblox') --> Roblox is cool!
module:example() --> This is a test