How to add custom event to tables?

Is there a way to achieve:

table.event:Connect(function(some_value)

end)

?

1 Like

The question is pretty vague …
You could iterate through the table and key off that.
example, example

2 Likes

Ok I saw this code:

local icon = require(modules:WaitForChild('Icon'))
local icon = icon.new()

icon.selected:Connect(function()
end)

How do I do that?

3 Likes

I’m sure that don’t even do “that” … icon is getting defined twice.
That is calling a module script for use.

A table is like an array, but with tables you can add most any type of data … even a objects.
So, if I have a bunch of objects in my table I can then iterate through the table checking values.
Like in them examples, they are then keying off what is returned as it iterates.

1 Like

Yeah but I still don’t understand about the

icon.selected:Connect(function()
1 Like

no you cannot create custom events

doing Icon.new() must be returning something that has the ‘Selected’ event

That is attempting to do a module script command from a normal script. It also looks to have many errors. That is not a table.

the selected is coded like this inside the module script

self.selected = maid:give(Signal.new())

this is the give function

function Maid:give(taskOrPromise)
	local taskId
	if type(taskOrPromise) == "table" and taskOrPromise.isAPromise then
		_, taskId = self:givePromise(taskOrPromise)
	else
		taskId = self:giveTask(taskOrPromise)
	end
	return taskOrPromise, taskId
end

this is the icon module TopbarPlus - Roblox

Still not sure what you’re looking to do here … If they pick a taskOrPromise you could add code right before the else that could trigger an event. OR I see it is also returning the values. So you could add a variable to that call then use that to test for triggering an event.

It looks like this module is covering something like that now. There is too much code missing for me to even answer anything here. You got me guessing what you’re looking for.

I’m going to step out of this one as my answers could be way off here.

1 Like

Contrary to the previous replies, you can create custom events. There’s a few event Modules that you can use such as GoodSignal (recommended) or you can use BindableEvents.

-- Example
local t = {}
local event = Instance.new("BindableEvent") -- create the event

t.CustomEvent = event.Event -- set the event property to the RBXScriptSignal from the Bindable

-- Connect to the event
t.CustomEvent:Connect(function(...)
    print(...) -- prints 1 2 3
end)

-- then you would fire the event at some point
event:Fire(1, 2, 3)
3 Likes

You can use Bindables for that

local test = {}

local bindable = Instance.new("BindableEvent")

test.event = bindable.Event

workspace.ChildAdded:Connect(function(child)
   bindable:Fire(child)
end)

test.event:Connect(function(inst)
   print(inst)
end)

You can either use BindableEvents (which I don’t recommend since it is extremely easy to create memory leaks if you dk what you are doing) or custom Signal module like Stravant’s GoodSignal. Considering you have a Util folder in ReplicatedStorage, just pop this module into it and name it Signal. Then in the module:

local Signal = require(ReplicatedStorage.Util.Signal or wherever you stored the module)

local Module = {
    Event = Signal.new(),
}

Module.Event:Connect(function(message: string)
    print(message) -- prints "Hello, world"
end)

Module.Event:Fire("Hello, world")
Module:DisconnectAll() -- remember to disconnect the events when you no longer need it

You can make

local table = {}
local event = Instance.new("BindableEvent")
table.event = event.Event
table.__event = event

table.event:Connect(function()
     --do something
end)

task.delay(5,function()
      table.__event:Fire()
end)

too late for that but anyway.

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