#Making a script more efficient

1 messages · Page 1 of 1 (latest)

cursive wraith
#

For every server block, I have a code that every second has a 50% chance of turning a light on or off.

local lights = script.Parent

local light_table = {lights.One, lights.Two, lights.Three}

while true do
    task.wait(1)
    local a = math.random(1,2)
    local b = math.random(1,2)
    local c = math.random(1,2)
    if a == 2 then
        light_table[1].Color = Color3.new(1, 1, 1)
    elseif a == 1 then
        light_table[1].Color = Color3.new(0, 0, 0)
    
    end
    if b == 2 then
        light_table[2].Color = Color3.new(1, 1, 1)
    elseif b == 1 then
        light_table[2].Color = Color3.new(0, 0, 0)

    end
    if c == 2 then
        light_table[3].Color = Color3.new(1, 1, 1)
    elseif c == 1 then
        light_table[3].Color = Color3.new(0, 0, 0)

    end
end```
#

However, it is really inefficient. How can I make it more efficient?

rough flint
#

how about you make the random number either 0 or 1 and remove the if statements

rough flint
#

this is the whole code

local lights = script.Parent

local light_table = {lights.One, lights.Two, lights.Three}

while true do
    task.wait(1)
    local a = math.random(0,1)
    local b = math.random(0,1)
    local c = math.random(0,1)
        
light_table[1].Color = Color3.new(a, a, a)
        light_table[2].Color = Color3.new(b, b, b)
light_table[3].Color = Color3.new(c, c, c)

end```
strong goblet
#

U can also simply iterate through the lights_table (credit to ChatGPT):

local lights = script.Parent
local light_table = {lights.One, lights.Two, lights.Three}

while true do
    task.wait(1)
    for _, light in ipairs(light_table) do
        local val = math.random(0, 1)
        light.Color = Color3.new(val, val, val)
    end
end
#

However, I think the actual inefficiency in this code is having a script for each light

#

What you probably want is a single script that uses CollectionService to track all the lights

cursive wedge
#

(put all da lights in a folder) 📂

vestal ginkgo
strong goblet
#

it just doesnt use a for loop

vestal ginkgo
#

yea but sperately

#

for each block

strong goblet
#

if u unravel the for loop it does the same thing

vestal ginkgo
#

oh ipairs

#

lemme check something

strong goblet
#

nothing to do with ipairs specifically, just a regular for loop

vestal ginkgo
#

nah im just checking what ipairs used for

strong goblet
#

ipairs is specifically for arrays

#

pairs is for arrays and dictionaries

vestal ginkgo
#

idk what arrays are

strong goblet
#

not using ipairs or pairs at all is for custom iteration

#

or generic iteration

vestal ginkgo
#

I never used it

strong goblet
#

It's data structure and algorithms

#

you can learn it online

#

but that's basically what you learn in a computer science college major

#

In this case, array is basically a table but the indices are numbers

#

dictionary is a table but the indices can be strings or something different than a number

vestal ginkgo
#

oh alright

#

yea I might have to look up on YouTube as well

#

and do more re searches about it

#

thanks tho

strong goblet
#

there are puzzles online you can do to learn more about data structures and algorithm (DSA)

#

using neetcode or leetcode

#

that's how big tech companies test your coding knowledge currently

#

if ur interested in that

vestal ginkgo
#

yea

#

gonna switch to a school soon anyway

cursive wraith
# strong goblet U can also simply iterate through the lights_table (credit to ChatGPT): ```lua ...

Im using the collection service, using this script

local CollectionService = game:GetService("CollectionService")

local Lights = CollectionService:GetTagged("Light")


local light_table = {Lights.One, Lights.Two, Lights.Three}

while true do
    task.wait(1)
    local a = math.random(0,1)
    local b = math.random(0,1)
    local c = math.random(0,1)

    light_table[1].Color = Color3.new(a, a, a)
    light_table[2].Color = Color3.new(b, b, b)
    light_table[3].Color = Color3.new(c, c, c)

end

but it isnt working

strong goblet
#

Yeah, that code isn't right, but let's take this step by step

marble masonBOT
#

studio** You are now Level 2! **studio

strong goblet
#

@cursive wraith Did you tag each Lights model with the Light tag

#

You could either tag each Lights model or just tag each actual Light (i.e One Three Two)