How to create an event?

I have looked over the forum and haven’t found anything to my problem. I am wondering how to create an event like ProximityPrompt.Triggered:...? The result should be Binding.Attachment:...

What are you trying to do? Triggered is an event. You could do

ProximityPrompt.Triggered:Connect(function()
--stuff
end)

I’m trying to emulate the behavior of said event and or events in order to create my own

You can create your own events using the Bindable Event, unfortunately you cannot create your own events built directly into the proximity prompt’s properties.

1 Like

Well, I don’t want to create it from ProximityPrompt.Triggered:..., I am using it as an example to what it is based off of. I want to create my own event without it being built directly into anything

You can bind Instance.new('BindableEvent').Event to some value in a modulescript.

Modulescript code:

local module = {}
module.bind = Instance.new('BindableEvent')
module.something = module.bind.Event
module.fire = function(...)
	module.bind:Fire(...)
end
return module

usage:

local module = require(somemodule)

module.something:Connect(function(...)
	print(...)
end)
module.fire(1,2,3)

Edit: there is a better way to do this with OOP but that would take me a bit to write and im too lazy to

Like the others said, you could use a bindable, but module.event.Event:Connect sounds a bit odd and it might not be great on memory.

I would recommend making a system with OOP like @StingyDudeman65 suggested.

local CustomConnection = {}
CustomConnection.__index = CustomConnection

function CustomConnection:Connect(func: () -> (...))
    if type(func) ~= "function" then
        error("Expected function.")
    end

    table.insert(self._ConnectedFuncs, coroutine.wrap(func))
end

function CustomConnection:Fire(...:any)
    for _, func in next, CustomConnection._ConnectedFuncs, nil do
        --resume each function asynchronously (it was wrapped in a coroutine)
        func(...)
    end
end

function CustomConnection.new(name: string): CustomConnectionType
    local self = setmetatable({}, CustomConnection)
    self._ConnectedFuncs = {}
    --blah blah
    return self
end

--yes ik you could just set to RBXScriptConnection but you aren't making the exact class so u want it to be accurate
export type CustomConnectionType = {
    ["Connect"]: (CustomConnectionType, () -> ()) -> (nil) --idk how to make return a representation
    --etc.
}

--add more RBXScriptConnection methods as needed
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.