What do you want to achieve? Keep it simple and clear!
Basically to keep it simple. Im trying to recreate the deprecated mosue function “.KeyDown” with OOp.
What is the issue? Include screenshots / videos if possible!
But i kinda ran into a wall atm . I need the “Module.KeyDown” be able to do something like this:
Module.KeyDown:Connect(function(key)
end)
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Ive done some diggin about how you can create custom events. And i think i get how they work pretty good. However im not 100% ive done it right?
Heres the code i use inside my Mouse module:
I also want to clarify before you start readin my code. I started learning OOP back in 2021 so im nowhere near a pro but im decent i guess. Alr you can continue now (
local Mouse = {}
local Signal = require(script.Signal)
Mouse.__index = Mouse
function Mouse.new()
return setmetatable({},Mouse)
end
function Mouse.KeyDown()
local NewSignal = Signal.new()
return NewSignal
end
return Mouse
And heres the Signal module
local Signal = {}
local Connection = {}
Signal.__index = Signal
Connection.__index = Connection
function Signal.new()
return setmetatable({},Signal)
end
function Signal:Connect()
return Connection.new()
end
function Connection.new()
return setmetatable({},Connection)
end
function Connection:Disconnect()
end
return Signal
Events of your object should just be signal objects, not functions, and since events are generally just properties (unless you’re creating a system to call certain functions with an identifier), they should be defined inside of the constructor.
If you’re looking to create class methods (functions), you should define that in your mouse module along with your metamethods.
local signal = require(script.Signal)
local mouse = {}
mouse.__index = mouse
function mouse:FireKeyDown(key) -- class (mouse) method
return self.KeyDown:Fire(key)
end
function mouse.new()
local self = {} -- self is the new mouse object
self.KeyDown = signal.new() -- again, since objects' events are just properties, we define them inside of the constructor
-- if you wanted any other properties (like name), you could add those here.
return setmetatable(self, mouse)
end
return mouse
Also, in your signal module you aren’t really creating a signal, you’re just creating blank objects that create one another, thus your signal wouldn’t ever fire. If you want help with that let me know.