Better way than having a ton of "events?"

I’m currently having the following issue:

X.Triggered:Connect(function()
..
end)

Y.Triggered:Connect(function()
..
end)

Z.Triggered:Connect(function()
..
end)
...

Is there a better way of doing this?

1 Like
function something()
	return
end

X.Triggered:Connect(something)
Y.Triggered:Connect(something)
Z.Triggered:Connect(something)

Providing the connected functions are all the same, if they’re not you can just use conditionals inside the definition of the function itself to differentiate between triggered elements.

1 Like

Uh that doesn’t really work for me because I have to check if one of many values gets changed.

1 Like

This should answer your question.

The code you provided is not an issue, there’s nothing wrong in doing that.
However, take a look at the link I provided above, it mainly depends on preference.

1 Like

Too many events will not damage performance. Just make sure to disconnect them when they are no longer needed, if they once will be no longer needed.

1 Like

It’s an idea more than anything, if all functions have a common base, but change something small, like an instance, you can do something like this

function Connect(String, Number)
	return function()
		print(String, Number)
	end
end
X.Triggered:Connect(Connect("X", 12))
Y.Triggered:Connect(Connect("Y", 1))
Z.Triggered:Connect(Connect("Z", 6))

instead of this

X.Triggered:Connect(function()
	print("X", 12)
end)

Y.Triggered:Connect(function()
	print("Y", 1)
end)

Z.Triggered:Connect(function()
	print("Z", 6)
end)
1 Like
local stuff = {X, Y, Z}
local function e()
   print("e")
end
for i, v in ipairs(stuff) do
   stuff.Triggered:Connect(e)
end
1 Like