Hello all,
This is a bit of a weird one (kinda abstract?) I don't think it really matters in dy-to-day use but I'm just curious. On a recent thread three approaches were suggested (see below).
My question is, focusing on how the script is listening to game and acting on the code (regardless of the usefulness/efficiency of the code inside being run) which method causes the least performance issues? (is it better to have listeners for many events, one event, or use infinit loops to determine status?)
- Updating playercount and acting via PlayerAdded and PlayerRemoved:
local Players = game:GetService("Players")
local door = script.parent
local function checkPlayers() --check if the door should be open and act upon it
if #Players:GetPlayers() >= 2 and door.CanCollide == true then --if there are more than two players and the door isnt open, open the door
wait(40)
door.CanCollide = false
door.Transparency = 1
elseif #Players:GetPlayers() < 2 and door.CanCollide == false then --if there arent enough players and the door is open, close the door
door.CanCollide = true
door.Transparency = 0
end
end
Players.PlayerAdded:Connect(checkPlayers) --everytime a player joins...
Players.PlayerRemoving:Connect(checkPlayers) --everytime a player leaves...
- Updating playercount and acting via Heartbeat:
[ADAPTED: the original post has been edited to exemplify the question]
game:GetService("RunService").Heartbeat:Connect(function()
if #game.Players:GetChildren() >=2 then
script.Parent.Transparency = 1
script.Parent.CanCollide = false
elseif #game.Players:GetChildren() <2 then
script.Parent.Transparency = 0
script.Parent.CanCollide = true
end
end)
- updating playercount and acting via an Infinite loop:
local Players = game:GetService("Players")
local door = script.Parent
while true do
if #Players:GetPlayers() == 2 then
door.CanCollide = false
door.Transparency = 1
end
wait(.01)
end
I hope you can make sense of my question. Thank you for taking time to read this!

7 comments
and the fact that there are more things listening "from" script 1 doesn't make a difference? Now that I think about it, I suppose the question is also how do listeners work... are they themselves hidden loops waiting for something to happen?
no, you give them a function and when an action happens for instance here when a player joins a game, they fire the function for you
I know this sounds dumb but...how does it know when a player joins? In terms of LUA as a language how do events fire...from my perspective it looks like the function is doing "while true do repeating wait() until action" ?? ohh, or is it more like someone transmitting an FM signal (action) through the air (game) and listeners are passively hearing the noise because they are tuned into the station?
I feel like that though process kinda answered itself at the end there but thought id post it for others thinking the same thing :)
no, in case of playeradded what might happen behind the scenes (i dont know this at all but its so you might understand how events work) the client sents a request to the server (he clients wants to join the server), server checks if he is allowed to join (server not full etc,) connection establihsed, now it fires playeradded and it creates the character for that player which fires characteradded
so it never really waits, its just responds to actions, like when player joins it calls the function that fires the event, like look into bindeable events where you can create custom events and you ll see you dont need to wait at all, you make it fire the function after an action happens
so for example what roblox might do for characteradded is, https://pastebin.com/pDpNfbTu this is just an example its not real its so that you can understand that evenst just get called after an action happens