Would having 1000+ .Changed events give a performance drop?

This is for a game that could have many buildings and potentially 1000+ lights

I am implementing a day/night cycle into my game. The day does not progress linearly, it simply has two states - day and night which will transition over a 5-10 second period every 5 mins.

One way of turning on all lights at night would be to iterate through every plot, find every light and enable it. The issue with this is say a player places an object at night or joins during the night, I need to have code there as well to toggle those new lights accordingly.

Another method is including this local script inside every light part

game.Lighting.Changed:Connect(function()
-- Turn all lights on/off depending on time
end)
-- Toggle lights depending on situation When script is run

This is a lot easier as each individual light will handle itself.

However, would the second method give the game a massive performance drop? As there could be 1000+ listeners waiting for the .Changed event?

[The reason i’m asking this is to cut down on code complexity]

3 Likes

you should try having one .changed and a loop that looks for all the lights

not sure about performance but it would probably be a lot cleaner

2 Likes

Surely you could just have 1 event on the server and only fire when the lights need to be turned on or off and have a script in every light that listens to this event. This would be more efficient then listening to game.Lighting.Changed for every light, but still not as efficient as having one script handle it.

3 Likes

have a script in every light that listens to this event

I’m not too sure how the Roblox engine works behind the scenes, so I guess my question is would this give a large performance decrease?

but still not as efficient as having one script handle it

And yeah I agree that’d be much more efficient. I’m just trying to cut back on code complexity as the system needs to deal with new items appearing at any time.

1 Like

It would have a slight performance impact, mostly just using up more memory, but realistically it wouldn’t use much CPU time apart from when the lights need to be toggled. It is a lot less resource intensive of listening too the game.Lighting.Changed in every light.

2 Likes

I’d wager otherwise. Every time Lighting changes you run 1000 threads. If OP decides to have more lights, that’s more threads and more scripts possibly encumbering things.

This is generally just a bad idea. The better solution is handling it from 1 script and just checking if it’s already night or day when placing a new light.

3 Likes

I did end up going for a one script approach with a tonne of checks littered around my code to make sure lights are always in the correct state.

If you have a lot of repeated code for checking or just light related things you might find yourself being able to cut down on it by factoring it out into a ModuleScript. You can always see if it helps.

1 Like

Yeah if you listened to lighting.Changed in every script, but if you only checked lighting.Changed in one script and used an event to fire when the lights need to be on or off, although not the best solution, it would only run those 1000 threads for a very short amount of time.

Still not great, but it is better than listening to every Changed even 1000 times

Would using CollectionService work for this?

3 Likes

Yes! This is what I would do.

Use a single CollectionService tag for all your lights. You can set the tags for the already existing lights in Studio; if you’ve already placed a ton, just run a command that loops through them all and sets their tag.

Have a single script handle changing them. There, you could do a Lighting.Changed connection, and use CollectionService:GetTagged() to loop through all of the lights and change them. Also set up a CollectionService:GetInstanceAddedSignal() connection for lights that people place, and just set the tag of the light in whatever script is responsible for placing it.

If you want to edit tags in Studio, I highly recommend this plugin. You can read up on CollectionService here.

Hope this helps!

3 Likes