Simple Street Light Script! [Beginners Tutorial]

Here is my beginners tutorial for street light scripts!
image

This script makes a parts light turn on depending on the time, acting like a street light.

   while true do
	if game.Lighting.ClockTime <=6 then --<= means less than or equal to 6
		script.Parent.SurfaceLight.Brightness =16 --Light Brightness
	else
		script.Parent.SurfaceLight.Brightness =0 --Turns Off
	end
	task.wait(0.3)--Prevents Lag
end

Confused? Here I will explain.

Part1

First create a part in the workspace!
image
Next insert a surface light in side of the part.
image
After that make sure to also insert a script in the part.
Make sure the Surface Light’s brightness is set to 0!
image
Pro Tip: You can see where the light goes when you click on surface light in the part.
Nice job so far! Now we can go on to part 2! Here comes the scripting! :grinning_face_with_smiling_eyes:

Part2

First we have these two lines.
image

What these two lines basically do is they constantly check of the clock time is less than or equal to 6.
After that line you will soon see this line.
image
This line sets the Surface Lights’s brightness to a specific number. I just put 16 for example. If you set it to 0 the Surface Light will not emit any lights.
After that line you will see a image .
If the Clock time you saw earlier is not less the or equal to the set time it will, then it will go on to else instead of just ending the script.
After that else you will see a wild image !
This basically sets the Surface Light’s brightness to 0 which makes it emit no light.
Finally you will see this image .
The wait(0.3) Just adds a delay before re-reading the script. Making this delay set to nothing can start some lag so be aware! Then it ends here!

Notes :spiral_notepad:

This tutorial is ment for beginner scripters so dont worry you can modify all the numbers listed here!

Numbers

image

Also this is written in a normal script not a local script!
To change the clock time by the way, you can do it by going to lighting>clocktime
Heres a image of it.

Thanks for seeing and using my tutorial!
Sincerely, John

14 Likes

Here is the download link!

No credit needed :grinning_face_with_smiling_eyes:

3 Likes

In the future, you probably don’t want to use a while loop to check if property changes.

If you want to do something when a property changes there’s always :GetPropertyChangedSignal.

So in the case of this script you can do something like this

local lighting = game:GetService("Lighting")

lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
    if lighting.ClockTime <=6 then --<= means less than or equal to 6
		script.Parent.SurfaceLight.Brightness =16 --Light Brightness
	else
		script.Parent.SurfaceLight.Brightness =0 --Turns Off
	end
end)
9 Likes

Hello, since I’m begginer scripter I have a question. Why do you use script.Parent, you are doing this just to don’t use this: game.Workspace.Part.SurfaceLight, if no then why you are using script.Parent?

1 Like

Its because if you have multiple parts called “Part” then it won’t know which part to choose

If u use script.Parent it chooses whatever object script is parented to

1 Like

Although this is a very neat tutorial I personally don’t suggest doing this method entirely such as

  • While true do loops
  • You aren’t checking if the time is +18 or -6, Basically telling the script to only run when the time strikes 12 - 6 am
  • Instead of easily updating all the objects with one script you have every object contain a script, I personally don’t suggest having any scripts in the workspace but that’s just how I was taught by my brother.

What you’ll need

For my suggested tutorial on this, you’ll need a few basic things

Setting up

Reminder - It is always best to keep your game organized, I will help assist in that through this tutorial.

  • Create a folder in your workspace called “StreetLights”
  • Make a Script called “StreetLightsHandler” and parent that to ServerScriptService
  • Install the plugin called Tag Editor and use it to add a Tag to all your street light objects.

You can call your newly created tag whatever you please just remember to update the name in Settings.TagName
After we have all our objects tagged we can continue to the next step!

Into the Editor

Open the newly created script you made inside ServerScriptService and start with your variables.

local CollectionService = game:GetService("CollectionService")
local LightingService = game:GetService("Lighting")
local Settings = {
	TagName = "StreetLights",
	NightStart = 18,
	DayStart = 6,
}

These will help assist us later in our code.
Next, we’ll need to have it update our lights whenever the time changes.
Only having our Function update helps with performance since your not running a thread constantly.

First, we need to create our function to identify light objects of any kind.
This will return us the light source we need, If it doesn’t find anything it’ll return nil.

local function FindLight(Object: Instance):Instance
	return Object:FindFirstChildWhichIsA("SpotLight") or Object:FindFirstChildWhichIsA("SurfaceLight") or Object:FindFirstChildWhichIsA("PointLight")
end

Now let’s continue on to creating our function, this will handle everything.

function UpdateLights() -- Our function we will use to handle everything.

end

Now a function alone won’t do anything, so we’ll need to loop through our CollectionService using GetTagged, This will return an array of objects that we can loop through to use to easily identify our StreetLight objects.

But if we just change values all willy nilly without knowing what it is that might cause us some errors, So we will have to check if its a BasePart

function UpdateLights()
	for _, Light in ipairs(CollectionService:GetTagged(Settings.TagName)) do
		if Light:IsA("BasePart") then

		end
	end
end

But that in itself does nothing except check if it’s a part of some type? So now we’ll need to go on checking the time and updating the objects we got from CollectionService

function UpdateLights()
	for _, Light in ipairs(CollectionService:GetTagged(Settings.TagName)) do
		if Light:IsA("BasePart") then
			if LightingService.ClockTime > Settings.NightStart or LightingService.ClockTime < Settings.DayStart then
				local LightInstance = FindLight(Light)
				Light.Material = Enum.Material.Neon
				
				if LightInstance ~= nil then
					LightInstance.Enabled = true
				end
			else
				local LightInstance = FindLight(Light)
				Light.Material = Enum.Material.SmoothPlastic
				
				if LightInstance ~= nil then
					LightInstance.Enabled = false
				end
			end
		end
	end
end

But its a shame our script only runs once :sad:
So let’s change that but connecting our function to LightingChanged

LightingService.LightingChanged:Connect(UpdateLights)

Now let’s put our entire script together

local CollectionService = game:GetService("CollectionService")
local LightingService = game:GetService("Lighting")
local Settings = {
	TagName = "StreetLights",
	NightStart = 18,
	DayStart = 6,
}

local function FindLight(Object: Instance):Instance
	return Object:FindFirstChildWhichIsA("SpotLight") or Object:FindFirstChildWhichIsA("SurfaceLight") or Object:FindFirstChildWhichIsA("PointLight")
end

function UpdateLights()
	for _, Light in ipairs(CollectionService:GetTagged(Settings.TagName)) do
		if Light:IsA("BasePart") then
			if LightingService.ClockTime > Settings.NightStart or LightingService.ClockTime < Settings.DayStart then
				local LightInstance = FindLight(Light)
				Light.Material = Enum.Material.Neon
				
				if LightInstance ~= nil then
					LightInstance.Enabled = true
				end
			else
				local LightInstance = FindLight(Light)
				Light.Material = Enum.Material.SmoothPlastic
				
				if LightInstance ~= nil then
					LightInstance.Enabled = false
				end
			end
		end
	end
end

LightingService.LightingChanged:Connect(UpdateLights)

If you learned anything new or have any questions leave a like or reply, your choice.

16 Likes

Thank you! I understand now, it will help me in many scripts :grinning:

Please do not use while true do for something like this, as its slow. Try using :GetPropertyChangedSignal().

1 Like