Street Light Script

I’m trying to create a street light with minimal lag. I have a question about the scripting, like which one would lag more, a script that each street light has, or one script that activates them all when it turns to night, and how would I go about creating this because I’m not an experienced scripter.

You should adhere to the Don’t repeat yourself principle as much as possible. That means instead of copying one script into a thing (thereby creating multiple representations of the objects’ behavior), you should have one chunk of code manage them all. Whether that’s a Script inside the object invoking a ModuleScript, or just one Script doing a :GetChildren() on a Folder of those objects, or even a CollectionService tag is up to you.

More philosophically: worrying about performance this early on is a hinderance. Make it work, then make it work well. You will benefit more as a programmer overall if you approach your learning in this way. Just go for it!

And to answer your question more specifically, here’s how I’d do it:

  • Create a ModuleScript in ReplicatedStorage called LightManager. Make a function in it like LightManager.init(model), and inside that do what I’d do normally do to make a single street light model work as expected (listening to Lighting.TimeOfDay or something)
  • In each light model, add a Script which requires the LightManager module and calls LightManager.init(script.Parent) or similar. Since this script is really simple, there’s not much harm in copying and pasting it to a bunch of lights - it’s not likely to change, where as the LightManager will.
1 Like