Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Traffic light not working as intended?

Asked by 2 years ago

Hey! I'm hallow. I'm a beginner scripter within the ROBLOX platform, and I'm trying to improve my skills within coding so that I can make games on my own in the future. I thought creating a traffic light would be " simple ", so I gave it a try.

Here is my code:

--//Variables
local R = script.Parent.TrafficLight1.Red
local O = script.Parent.TrafficLight1.Orange
local G = script.Parent.TrafficLight1.Green
local R2 = script.Parent.TrafficLight2.Red
local O2 = script.Parent.TrafficLight2.Orange
local G2 = script.Parent.TrafficLight2.Green
local R3 = script.Parent.TrafficLight3.Red
local O3 = script.Parent.TrafficLight3.Orange
local G3 = script.Parent.TrafficLight3.Green
local R4 = script.Parent.TrafficLight4.Red
local O4 =  script.Parent.TrafficLight4.Orange
local G4 = script.Parent.TrafficLight4.Green
--\\Variables

--//OG colors
--[[
Red = Maroon
Orange = CGA Brown
Green = Dark Green
]]--
--\\ OG colors


--//Script

while true do

    --Traffic Light 1
    G.Material = Enum.Material.Neon
    wait(25)
    G.Material = Enum.Material.SmoothPlastic
    wait()
    O.Material = Enum.Material.Neon
    wait(4)
    O.Material = Enum.Material.SmoothPlastic
    wait()
    R.Material = Enum.Material.Neon
    wait()
    --\\Traffic Light 1 end
    if G.Material == Enum.Material.Neon or O.Material == Enum.Material.Neon then
        --Trafficlight2
        R2.Material = Enum.Material.Neon
        wait()

    elseif  R.Material == Enum.Material.Neon  then
        G2.Material = Enum.Material.Neon
        wait(25)
        G2.Material = Enum.Material.SmoothPlastic
        wait()
        O2.Material = Enum.Material.Neon
        wait(4)
        O2.Material = Enum.Material.SmoothPlastic
        wait()
        R2.Material = Enum.Material.Neon
        wait()
        --\\TrafficLight2 end

        if G2.Material == Enum.Material.Neon or O2.Material == Enum.Material.Neon then
            --TrafficLight3
            R3.Material = Enum.Material.Neon
            wait()

        elseif R2.Material == Enum.Material.Neon then
            G3.Material = Enum.Material.Neon
            wait(25)
            G3.Material = Enum.Material.SmoothPlastic
            wait()
            O3.Material = Enum.Material.Neon
            wait(4)
            O3.Material = Enum.Material.SmoothPlastic
            wait()
            R3.Material = Enum.Material.Neon
            wait()
            --\\Trafficlight 3 end

            if G3.Material == Enum.Material.Neon or O3.Material == Enum.Material.Neon then
                --TrafficLight4
                R4.Material = Enum.Material.Neon

            elseif R3.Material == Enum.Material.Neon then
                G4.Material = Enum.Material.Neon
                wait(25)
                G4.Material = Enum.Material.SmoothPlastic
                wait()
                O4.Material = Enum.Material.Neon
                wait(4)
                O4.Material = Enum.Material.SmoothPlastic
                wait()
                R4.Material = Enum.Material.Neon
                wait()
                --\\TrafficLight4 end


            end

        end

    end

end

The code " works ", as in the colors change and all in the proper order, however when the final light (TrafficLight4) turns red, it goes back to the first traffic light; which does turn green, but the red light is still neon. How do I make it so that when the last traffic light turns red, the first traffic light turns green instead of both green and red?

Thanks, and sorry if I wrote it badly.

1 answer

Log in to vote
0
Answered by 2 years ago

Hello there, you should set in line 28 that red light material should be smooth plastic.

R.Material = Enum.Material.SmoothPlastic

I actually looked at a possibility to make your script shorter and actually reduced it for couple of lines so take a look if you want to :)

I made traffic light models named TR1, TR2, TR3, TR4... (TR .. number) in a model called "Model" in the workspace. To make controlling them easier, I've made a for loop. Check it out.

while true do
    for i=1,4 do
        local currentTrafficLight = script.Parent:FindFirstChild("TR"..i) --ex. if "i" is 3, selected signal will be TR3
        local G = currentTrafficLight.G
        local O = currentTrafficLight.O
        local R = currentTrafficLight.R

        --orange light on
        R.Material = Enum.Material.Neon
        O.Material = Enum.Material.Neon
        wait(3)

        --green light on
        R.Material = Enum.Material.SmoothPlastic
        O.Material = Enum.Material.SmoothPlastic
        G.Material = Enum.Material.Neon

        wait(25) --wait time

        O.Material = Enum.Material.Neon
        G.Material = Enum.Material.SmoothPlastic
        wait(3)

        R.Material = Enum.Material.Neon
        O.Material = Enum.Material.SmoothPlastic

        wait(5) --wait time
    end
end

Make sure to ask if you don't understand something :)

Ad

Answer this question