Confused by what you mean there, this is usually the OOP structure
local Module = {}
Module.__index = Module
function Module.new(coins)
local self = setmetatable({}, Module)
self.Coins = coins
return self
end
function Module:foo()
print(self.Coins) --> 500
end
return Module
For example
local NewCoins = Module.new(500)
print(NewCoins:foo())
Yeah so what I want is basically like a bindable function where you can do a Function:Connect(function(args) but instead doing this with a module, I am attempting to make it so someone can initialize a prefix for a command and then use a .OnRun() every time that a user runs that command.
kind of an idea on what i’m attempting to accomplish. I’m not very good at explaining so if there is anything you’re confused on let me know.
local ranEvent = Instance.new("BindableEvent")
easyCmds.Ran = ranEvent.Event
Connecting to the signal:
easyCmds.Ran:Connect(function(command)
print(command .. " was ran")
end)
Firing the event:
ranEvent:Fire("help")
Keep the BindableEvent itself private to the ModuleScript as it has the power to fire. Only expose the BindableEvent.Event, which is the object used to make connections.
I understand that for in order for this to work I would need to allow the serverscript not the module to tell the module script the name of the command they want to create the event for.
I was trying to do this myself a couple of days ago, and it essentially boils down to creating the event functionality yourself. Here’s a rough version of what you can do.
In the module script:
local Thing={} --this is the module
local Foo={_functions={}} --this is the event
Foo.__index=Foo --make it a metatable
function Foo:Connect(f) --this is the connection event that all events use, taking the function
table.insert(Foo._functions,f) --adds function to the list
end
function Foo:connect(f) return Foo:Connect(f) end
--this just gives the same function another name
--it's optional support for my barbaric camel casing
function Foo:Fire(currentTime) --this can be used to trigger the event, internally or from another script
for i,v in pairs(Foo._functions) do
v(currentTime) --this part calls all of the functions connected
end
end
Thing.Foo=Foo --add event to module
return Thing
And on the other script:
local Thing=require(game.ReplicatedStorage.Thing) --get the module
function happened(ack) --function to connect, even receives arguments
print(ack)
end
Thing.Foo:Connect(happened) --connection to the event
wait(3)
Thing.Foo:Fire(tick()) --fire the event
Final result prints the current tick. It’s worth noting that the list of functions is still externally editable, so you’ll just have to hope nobody misuses the module and breaks it.
If you want a challenge, try also implementing event:Wait() and connection:Disconnect() to further emulate Roblox events.
I did something like this once. I can’t remember if I ever finished it. Event:Wait (with coroutines) and Connection:Disconnect are included. I added an extra Signal:DisconnectAll method too. https://hastebin.com/ebedahixay.txt