Making an Event in a ModuleScript

So, I have seen some ModuleScripts that have a Custom Event on them, I’m not sure if i was just a function or you can actually do this.

module.DoSomething:Connect() -- I saw something Like this

Is this even possible?

You can either use a custom signal module or use BindableEvents.

-- Example:

local t = {}
local event = Instance.new("BindableEvent") -- Create a bindable event
t.EventFired = event.Event -- Connect to the 'Event' property for the BindableEvent

t.EventFired:Connect(function(...)
    print(...) -- Print what was fired
end)

event:Fire(1, 2, 3, "Hello World!") -- Fire the event

I get that but I was referring to Events within Modules rather than Bindables (or at least without them)

Yes, modules use either BindableEvents or Custom Signal Modules, there aren’t any other ways. The example I showed is an event that can be connected to a ModuleScript:

-- Another Example
-- ModuleScript
local t = {} -- The ModuleScript information
local e = Instance.new("BindableEvent") -- BindableEvent (or a custom module)
t.Event = e.Event -- The Event Connection

task.delay(1, function()
   e:Fire(1, 2, 3) -- Wait a second then fire the event
end)

return t

-- A regular script
local module = require(ModuleScript)
module.Event:Connect(function() -- Connect to the event listener
  --...
end)

Unless I’m somehow misunderstanding?

Yep!
You can do this quite easily, first create a bindableEvent then return bindableEvent.Fired as the value of the variable.

Like so:

local playerAddedModule = {}
local playerAddedBind = Instance.new("BindableEvent")

playerAddedModule.onPlayerAdded = playerAddedBind.Event

game:GetService("Players").PlayerAdded:Connect(playerAddedBind:Fire)

return playerAddedModule
local module = require(PlayerAddedModule)
module.onPlayerAddedBind:Connect(funnction(player)

end)

Another way is metatables, but I wont get too in depth into that.

In your example, it would actual be:

local playerAddedModule = {}
playerAddedModule.onPlayerAdded = game:GetService("Players").PlayerAdded 
-- PlayerAdded is a ScriptSignal, so you can assign it to a property

return playerAddedModule

This would give you a syntax error as you cannot use “:” without explicitly calling it

So Basically, I’m asking if its Possible to create a Event without the usage of BindableEvents, I get that its a simple way to do it but I want to try without one, some Modules I’ve seen dont seem to use them and I want to try and do that.

If there is a way with metatables, I would like to know about it.

Would FastSignal or GoodSignal suffice?

Not really?

I would prefer to learn or get to understand some methods rather than using someone else’s code / module.

There’s no point in reinventing the wheel. Hundreds of creators use this module because it’s useful.

Making signal modules are complicated, as well as the type checking.

I get that, But I prefer not using other peoples stuff, I’d rather learn off of something.

Cool, that’s why I want to learn how to do it.

1 Like
2 Likes

That’s really all I needed to know.

Replicating RBXScriptSignals in Luau is rather hard, mostly because you need to understand A LOT. First of all, just things that aren’t documented that simply happen. (eg. thread recycling on Immediate mode.) You’ll need to understand linked lists and how to implement one in Roblox, and it is especially hard to deal with Immediate mode signals because a lot of silent issues can come from a poorly implemented Signal on Immediate mode, most issues come from code execution in connections changing properties while a signal is firing. It isn’t as much as an issue in Deferred mode where you can actually make otherwise bad implementations and it would work just or mostly fine.

1 Like

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