Can I detect multiple events at the same time?

Hello,
The title says all. Is it possible to do something like that?

Players.(PlayerRemoving or PlayerAdded):Connect(function()
    print("player count changed")
end)

Or that:

(button1 or button2).Activated:Connect(function()
    print("Clicked one of buttons")
end)

It would be really helpful and would let me write code more in DRY principle style.

Short answer: No
While you can’t detect events like that, you could connect them to the same function so only a single function deals with them both:

game.Players.PlayerRemoving:Connect(myFunction)
game.Players.PlayerAdding:Connect(myFunction)
1 Like

second idea you can add a Value like
local button1clicked = false
button1.clicked:Connect()
button1clicked = true
end)

local button2clicked = false
button2.clicked:Connect()
button2clicked = true
end)

for all buttons that will be in the same place:

for _,button in pairs(script.Parent:GetChildren()) do
	if button:IsA('TextButton') then
		button.MouseButton1Click:Connect(function()
			print(button)
		end)
	end
end
1 Like

This can be achieved by connecting the same callback function to two or more event objects via the event connection instance method β€œ:Connect()”, for the first example that would look like.

local Players = game:GetService("Players")

local function PlayerCountChanged()
	print("Number of players: "..#Players:GetPlayers())
end

Players.PlayerAdded:Connect(PlayerCountChanged)
Players.PlayerRemoving:Connect(PlayerCountChanged)

and for the second example.

local frame = script.Parent
local button1 = frame:WaitForChild("Button1")
local button2 = frame:WaitForChild("Button2")

local function onButtonPressed()
	print("Button pressed!")
end

button1.MouseButton1Click:Connect(onButtonPressed)
button2.MouseButton2Click:Connect(onButtonPressed)
1 Like