Utilities 5 min read

Build Lighting Toggles and a Day–Night Cycle

Control ambience on the server and add a personal flashlight that supports desktop and touch input.

Set a baseline in Lighting

Choose a morning ClockTime and compare it with midday and sunset. A clock cycle should derive its time from elapsed server seconds. This keeps sunrise smooth even if the script misses a scheduler tick. Pick a full cycle length that suits your level and compare it with your round length.

Attach a personal light

Add a SpotLight to the player's Head in a LocalScript and rebuild it when the character resets. Bind a toggle through ContextActionService for a keyboard input and touch action. Use modest brightness and range first, then test in a dark room with your usual lighting effects enabled.

Make dark routes discoverable

Put a landmark at the entrance and a bright destination near the exit. A flashlight should reveal nearby detail without being the only route marker. Check the level again in phone emulation and minimize effects that make the floor hard to see.

Set up a small lighting test scene

Build an outdoor path with three landmarks: an entry arch, a turn and a destination. Next to it, make a small room with a covered roof and a door. Use the same materials you expect to use in the full map, since bright surfaces and dark floors can read very differently under identical lights. Check the path at morning, noon and sunset by setting ClockTime manually in Studio. Pick one early morning value and one night value that still allow a new player to find the start. Only add the automatic clock once your map looks reasonable at those fixed times. This makes later problems easier to separate from the timer itself.

Calculate the hour from elapsed time

Set a desired day length in real seconds. Dividing elapsed seconds by that length gives the fraction of a full day. Multiply by 24 and wrap the result so the clock returns to the beginning after midnight. Pick a stable baseline time when the server starts. Computing the value from elapsed time makes the result independent of how often the update happens. Try a four-minute cycle first. Shortening it during development makes an entire cycle easy to inspect, but a very fast sunset in production can make a level confusing. Compare the length of one day with the intended length of a round or route before choosing values.

local elapsed = workspace:GetServerTimeNow() - began
local hour = (7 + 24 * elapsed / realSecondsPerDay) % 24
Lighting.ClockTime = hour

Keep the flashlight tied to the current player

Create the flashlight in a LocalScript and attach a SpotLight to the current character's head when it appears. Aim it forward and start with modest brightness and range. The light belongs to the player using it, so their light toggle should not change Lighting on the server for everyone. Bind an action that can display a touch button and listen for a keyboard control. Start with one toggle and check that it works before adding an energy meter or any sound. When a character is replaced, recreate the light under the new head and decide whether the toggle begins off or restores a local preference.

Look for readable silhouettes and useful cues

Walk the same path during day and night. Check whether a bench, a doorway and a turn can still be distinguished with the light off. Use small permanent fixtures at important direction changes. A flashlight is useful for revealing details, but a missing HUD button should never leave the player unable to locate the exit. In a room, evaluate the light from both far and close range. A large angle covers the whole corridor but reduces the sense of direction. A narrower angle draws attention strongly but can miss a nearby step. Tune range, angle and brightness one at a time and repeat the same route after each change.

Test late entry and a character reset

Start a multiplayer session and allow one client to join after the time has advanced. The new visitor should see the server's current lighting rather than restarting the day. Toggle the flashlight for one player while the other stays in the room to check that local controls remain personal. Reset with the light enabled and verify that the replacement character starts in the intended state. Then run the route in phone preview. If the light button overlaps another action, adjust its placement under ContextActionService or integrate it into your own controls. Keep the scene readable at low visual quality before adding bloom or other atmosphere effects.

Related scripts