I have an simple question for you regarding streetlights!

Hi there!

So, as the title says I have an simple question for you guys regarding streetlights.

This question is simply only for “educational” purposes only! I do not need any help on scripting streetlights etc. My bad if I put this in the wrong category, but it seems very logical to put it here.

Anyways, here is the question.

So, lets say that I want streetlights to automatically turn on at 6 PM and turn off at 6 AM. All streetlights need to turn off automatically at the same time.

What I want you guys to tell me if this is achieveable if I do it this way:

I will be using 2 scripts, both of them are located in ServerScriptService,

The first script tracks in-game time, controls the cycle of day and night within a 24-minute full cycle and manages the state of streetlights based on the in-game time. The script will include; logic to track the in-game time equating to a 24-minute real-time cycle, with each in-game hour represented by 1 real-time minute. It also includes checks for when the time is at 6 PM and 6 AM to turn the streetlights on and off.

The second script manages the activation state of each streetlight, ensuring they turn on or off based on the signals from the first script. This script will include references to specific streetlight objects within the game to control their on/off state. It will rely on receiving a signal or check from the first script to determine the current in-game time and adjust the streetlights’ states accordingly.

Now it seems to me that the 2nd script isn’t necessary. Though the first script does not include anything about controlling the streetlight objects. I mean how can the script turn the lights off when it doesn’t even know where the streetlights are located for example.

What are your thoughts about this? --Yes, I did not made this approach!

You approach is not technically wrong but I would say it’s unnecessary. Here’s what I would propose and do: I would keep the first script and turn the streetlights on or off from there. To get the location of each streetlight we have two options:

  1. Make a folder containing each streetlight

  2. Give each streetlight a specified tag that represents streetlights

Personally, I would go between a mix of the two, because I want to have a clean game but also efficient code. So I would make a folder containing each streetlight to keep them nice and tidy and also give them all a tag indicating that they’re a streetlight. Now to get all those objects that are tagged with our streetlight tag, we use the CollectionService. This service handles basically anything related to tags and using the :GetTagged() function, we can get all objects that have the given tag. Finally we will just have to loop over this returned table of objects and since we are getting returned a table of streetlights we simply have to turn them off or on from there. I think this is the best, simple and easiest solution to your question.

Documentation: CollectionService | Documentation - Roblox Creator Hub


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.