Traffic Light Scripting

Hi.

local TrafficLight = script.Parent
local Green = TrafficLight.Green
local Yellow = TrafficLight.Yellow
local Red =  TrafficLight.Red
local Materials = {Enum.Material.SmoothPlastic, Enum.Material.Neon}

local function TurnOn(Light)
	Light.PointLight.Brightness = 1
	Light.Material = Materials[2]
end

local function TurnOff(Light)
	Light.PointLight.Brightness = 0
	Light.Material = Materials[1]
end

while true do
	TurnOn(Red)
	TurnOff(Yellow)
	TurnOff(Green)
	task.wait(10)
	TurnOff(Red)
	TurnOn(Yellow)
	TurnOff(Green)
	task.wait(10)
	TurnOff(Red)
	TurnOff(Yellow)
	TurnOn(Green)
	task.wait(10)
end

This script works but when it goes green- yellow - red it skips the yellow? Any idea? I have changed the script but still is like that.

This is the script I am using currently?

Any idea how to not make the script skip yellow when it goes in order green-yellow red?

2 Likes

Does the yellow traffic light have a pointlight? Any errors in output?

This loop sequence is working for me(green, yellow, red):

while true do
	TurnOn(Green)
	TurnOff(Red)
	TurnOff(Yellow)
	task.wait(10)
	TurnOn(Yellow)
	TurnOff(Green)
	TurnOff(Red)
	task.wait(10)
	TurnOn(Red)
	TurnOff(Yellow)
	TurnOff(Green)
	task.wait(10)
end

Yes, it might be that the code itself is not the problem in OP’s question. It might be the traffic light itself.

If a PointLight was missing, the loop would break and stop completely, therefore I suppose that’s not the issue.

Maybe the PointLight is disabled? No clue. @CakeValxie can you send us screenshots of the traffic light in hte explorer?

Ohh sorry I misunderstood the post, in that case this should fix it:

--rest of your code and functions
local function Switch(Light) 
	for _, light in pairs({Green, Yellow, Red}) do 
		TurnOff(light)
	end
	TurnOn(Light)
end

while true do
	Switch(Red)
	task.wait(10)
	Switch(Yellow)
	task.wait(10)
	Switch(Green)
	task.wait(10)
	Switch(Yellow) 
	task.wait(10)
end

Pretty much you have to switch Yellow to ON at the end of the loop, the reason I created the Switch function was to avoid making the code snippet long and hard to read.

Hmm still seems to skips. I tried changing a few things but nothing fixes. :thinking:

local TrafficLight = script.Parent
local Green = TrafficLight.Green
local Yellow = TrafficLight.Yellow
local Red =  TrafficLight.Red
local Materials = {Enum.Material.SmoothPlastic, Enum.Material.Neon}

local function TurnOn(Light)
	Light.PointLight.Enabled = true
	Light.Material = Materials[2]
end

local function TurnOff(Light)
	Light.PointLight.Enabled = false
	Light.Material = Materials[1]
end

while true do
	TurnOn(Red)
	TurnOff(Green)
	task.wait(10)
	TurnOff(Red)
	TurnOn(Yellow)
	task.wait(3)
	TurnOff(Yellow)
	TurnOn(Green)
	task.wait(10)
end

try this, it should work!

1 Like

It still skips the yellow. I wonder what is the issue. No errors or warnings in the output.

1 Like

That’s weird… What type of script is it and where is it located?

Its a normal script and its located inside of the model.

Traffic lights do not go from green to yellow to red, hence the loop was created this way intentionally.

local TrafficLight = script.Parent
local Green = TrafficLight.Green
local Yellow = TrafficLight.Yellow
local Red =  TrafficLight.Red
local Materials = {Enum.Material.Metal, Enum.Material.Neon}

local function TurnOn(Light)
	Light.PointLight.Brightness = 8
	Light.Material = Materials[2]
end

local function TurnOff(Light)
	Light.PointLight.Brightness = 0
	Light.Material = Materials[1]
end

while true do
	TurnOn(Red)
	TurnOff(Yellow)
	TurnOff(Green)
	task.wait(10)
	TurnOff(Red)
	TurnOn(Yellow)
	TurnOff(Green)
	task.wait(10)
	TurnOff(Red)
	TurnOff(Yellow)
	TurnOn(Green)
	task.wait(10)
	TurnOff(Red)
	TurnOn(Yellow)
	TurnOff(Green)
	task.wait(10)
end

Simple change, not sure why others struggled.

1 Like

Finally! Thank you so much Forummer. You are very helpful. Thanks Infinite Times :+1: