I have hotbar and backpack(not fully implemented but has 40 spaces) slots and I want to check if they’re being clicked. But I’m wondering if lag will appear with all these connections.
local ScreenGui = script.Parent
local Backpack = ScreenGui.Backpack
local Hotbar = ScreenGui.Hotbar
local HotbarSlots = {
Hotbar.One,
Hotbar.Two,
Hotbar.Three,
Hotbar.Four,
Hotbar.Five,
Hotbar.Six,
Hotbar.Seven,
Hotbar.Eight,
Hotbar.Nine,
Hotbar.Zero
}
local function ConnectButtons()
for i, button in ipairs(HotbarSlots) do
connections[button] = button.Activated:Connect(function()
print("clicked")
end)
end
end
Is this the best way or does anyone have a better method?
I think you’d be fine with all these connections, you could run a test on studio and see if your ping or server load increases, but from what I’m seeing it doesn’t look like it would.
If I understand correctly, it would mostly depend on the code that is being run as a result of those connections.
On its own, having 40 button activation connections that don’t run any code or only run an individual print statement shouldn’t noticeably tank the performance, especially if those connections are cleaned up when they are no longer necessary.
As an example, if ResetOnSpawn is enabled for the ScreenGui, the player gets given a new ScreenGui every time they respawn, making the connections created in the old GUI unnecessary, since the player can no longer access the old GUI. As far as I understand, the connections would be cleaned up automatically if the objects that the events were connected to were destroyed.
However, if each of those buttons contained some intensive code (for an extreme example, pressing one button infinitely looped through every item in the Workspace and then made a whole bunch of modifications to everything), then that would definitely have a much more noticeable impact on the game’s performance, especially if the prior connections were not disconnected upon clicking new buttons.
I don’t have a super technical understanding of this subject, though, so if you’d like to learn more, I’d recommend reviewing the following resources (most of which aren’t specifically about event connections, but are adjacent and relevant to the topic):