Trying to recreate deprecated function with OOP

  1. 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.

  1. What is the issue? Include screenshots / videos if possible!

But i kinda ran into a wall atm :sweat_smile:. I need the “Module.KeyDown” be able to do something like this:

Module.KeyDown:Connect(function(key)

end)
  1. 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 (:slightly_smiling_face:

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

May i ask why? UserInputService is there and much better and it even has a similar function to keydown.

1 Like

Theres really no specific reason. I just wan to try to bring it back as a fun thing(Also as a fun challange) :slightly_smiling_face:

1 Like

Well you could recreate it using uis like i said but have fun! i don’t know why my post got deleted :slightly_frowning_face:

1 Like

Ah alr ill just go with this ugly solution for now. Ill mark this as solved until someone can help me in the future

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.

1 Like

Yoo thanks alot! now i no longer need to have those ugly brackets!

Yea im not a pro as you can see XD. All this oop shmuck still confuses me sometimes

1 Like