Im doing a module for my game and i’m trying to do something like
local module = require(Module)
local SetUp = module.Create()
SetUp.CFrame = Humanoid.RootPart.CFrame
SetUp:Start()
is there any way to do a start up to a function?
Im doing a module for my game and i’m trying to do something like
local module = require(Module)
local SetUp = module.Create()
SetUp.CFrame = Humanoid.RootPart.CFrame
SetUp:Start()
is there any way to do a start up to a function?
If you want this to work, you’ll need to return the part or whatever it is in the function of the module script
i alredy figured out how to change like the value of the things on the module script, but not starting it, i will try searching what you said.
Here’s an example of what you could do:
function module.CreatePart()
local part = Instance.new("Part")
part.Parent = workspace
return part
end
Hope it helps!
but like, how do i from the script send the “:Start()” at the function and it does something at the other side?
You could do this:
local module = {}
module.Create = function()
local self = {}
self.Instance = Instance.new('Part')
function self:Start()
print('Started')
end
return self
end
return module
basically it creates a table like thing, AKA self, which contains the instance, and the start function!
Thanks, i was looking for it but i could not find it
It looks like you are learning how to use module-scripts.
I just want to make sure you learn how to use them properly.
First, Module-Scripts are only meant to share data to other scripts. You don’t need to make your entire game work around modules. Please avoid using module-script to replace localscripts or serverscripts. It only complicate things.
For example. The module you are trying to create seems to use “reinvent” Instance.new() but with a few quirks.
Well, here’s what you could do to easily replace that:
local Part = Instance.new("Part")
Part.CFrame = Humanoid.RootPart.CFrame
Part.Parent = workspace
local function start()
end
Don’t use modules to make overcomplicate your code.
Now the proper way to use a module-script would be to share data, Here’s an example:
local stamina = {
value = 100,
max = 100,
min = 0,
}
return stamina
The code above is called a structure (struct for short).
This is a pretty good solution and mimics Object Orientation without the downside of psuedo-OOP in Roblox Luau! Also, pretty easy to understand as metatables can sometimes be pretty meta and weird.
Please do not try enforcing opinionated coding practices! Pure Functions are great and come with way less drawbacks in Luau than OOP (especially since OOP isn’t truly supported) but they both exist for purpose. Modules are good at interoperability and can easily be pinpointed and changed without needing to trail through long scripts. Both of these options are correct, and OOP is usually more correct for shared resources, but that doesn’t mean it has to be.
Threads about the pros and cons of both of these practices are pretty common and should be a nice read for OP, but only if they want to. They could also just be trying to practice something they’ve never experimented with before and should be encouraged to test things out and figure out what works best for them
I disagree. The coding practices I enforce are the standard programming methods that have been proven efficient but are unknown to Roblox developers due to learning from Roblox, who don’t bother to point them in the right direction for programming standards.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.