Light Chase


  • OH! What's That??

    What I made this time is a light chase, which different lights will light up in a specific sequence.
    (What I mean for lighting each light is to change the brick's material to Neon.)
    Making this is really awesome and I had fun with it! Hopefully, this will interest you too! :)

    How I Made it

    The Script

    Let me display you the script first:

    -- Script by CStarLam
    
    -- Instructions: Place at least 2 parts, each must be named WITH A NUMBER ON THE END!!! You can name anything before it. Also, make sure everything is in a model! If you are an expert, you can script your own pattern(s) as well. :)
    
    local model = script.Parent
    local lights = {}
    
    local WaitTime = 0.125 -- SET THE WAIT TIME YOURSELF!
    local Phase1LoopTimes = 2 -- Set the amount of loops to be made in phase 1. (It lights up each light in order per WaitTime seconds.)
    local Phase2LoopTimes = 2 -- You can also use "#lights" to represent the amount of lights in a table. This phase lights up the ones with the odd number tags (numbers at the end of each light name), then the even ones.
    
    wait(3) -- Gives player time to position the camera... maybe it's just me in that case. ^_^
    for _, object in ipairs(model:GetChildren()) do
    	if object:IsA("BasePart") then
    		table.insert(lights, object) -- Initialization: Inserts all the lights into the table.
    	end
    end
    
    table.sort(lights, function(E1, E2) -- Each light is named "Light#", with "#" being a number. The function compares two of the light names based on the number at the end. If the number is smaller, the first element goes first. Without the iterator, the script will crash here.
    	local pattern = "%d+"
    	local num1 = E1.Name:match(pattern)
    	local num2 = E2.Name:match(pattern)
    	if num1 and num2 and num1 < num2 then -- Double checking whether the "numbers" are nil or not.
    	    return true
    	else
    	    return false
    	end
    end)
    
    local function LightUp(LightPart)
        LightPart.Material = Enum.Material.Neon -- Neon makes things glow better!
    end
    
    local function LightDown(Light)
        Light.Material = Enum.Material.Plastic -- No glow on the light.
    end
    
    local function AllLightsDown()
    	for _, light in ipairs(lights) do
    		light.Material = Enum.Material.Plastic -- No glow on ALL lights.
    	end
    end
    
    while true do
    	for t = Phase1LoopTimes, 1, -1 do -- Loops twice.
    		for _, light in ipairs(lights) do -- Turns on each light. 
    			LightUp(light)
    			wait(WaitTime) -- Waits for the amount of time defined in the variable WaitTime.
    			LightDown(light)
    		end
    	end
    	
    	for t = Phase2LoopTimes, 1, -1 do -- Loops twice.
    		for i, light in ipairs(lights) do -- Red ones turn on.
    			if i % 2 == 0 then
    				LightUp(light)
    			end
    		end
    		wait(WaitTime)
    		AllLightsDown()
    		
    		for i, light in ipairs(lights) do -- Green ones turn on.
    			if i % 2 ~= 0 then
    				LightUp(light)
    			end
    		end
    		wait(WaitTime)
    		AllLightsDown()
    	end
    	wait(WaitTime)
    end
    

    Yes, the formatting was weird here... If you want to see the proper script itself, take one here: https://www.roblox.com/library/691369994/Light-Chaser

    Explorer Setup

    Also, here is a picture of how I set it up.
    0_1489289130496__SHLightChaserSetup.png
    As you can see, the setup is really nice and simple.

    Here is what it looks like.

    I hope you will find it interesting.

    CStarLam, signing off!


  • You can upload gifs.

Log in to reply
 

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.