How can I make custom events with OOP?

I see modules like:

local hitbox = hitbox.New()
hitbox.OnTouch:Connect(function()
end

I want to know how to make these custom events

1 Like

Heres an example:

local core = {}
core.__index = core
local events = {
   onTouch = Instance.new("BindableEvent")
}

function core.new()
    local self = setmetatable({},core)
    self.onTouch = events.onTouch.Event
    return self
end

return core

If you dont want to use bindable events then you can use Signal module

Thanrk you, and also can I ask, I just wanted to know why do people use self as a variable, does it make any difference if you use a different variable name?

for the new function, you do not need to declare the variable as ‘self’. for other functions in oop, you use self because it refers to the class itself

You can use any variable you want instead of using self in new().

In terms of the post you’re replying to, self is just a preference that programmers use for some reason. It doesn’t have to be used. I believe it is generally used because that is what other functions belonging to the class have to use, and it creates consistency.