Hello guys I need help on How Can Improve this code. I’m new in scripting on roblox and I’m learning here in Roblox Education. I just create a Light Color changing from Roblox Challenges. So I experiment on how to use loops.
local lightpart = script.Parent
local light = lightpart.SurfaceLight
local red = Color3.new(1, 0.333333, 0)
local white = Color3.fromRGB(0, 255, 255)
local purple = Color3.new(0.666667, 0.333333, 1)
local timechange = 0.5
while true do
light.Brightness = 40
light.Color = red
wait(timechange)
light.Brightness = 0
wait(timechange)
light.Brightness = 40
light.Color = purple
wait(timechange)
light.Brightness = 0
wait(timechange)
light.Brightness = 40
light.Color = white
wait(timechange)
light.Brightness = 0
wait(timechange)
end
It looks like, within the while true do loop, you’ve got a block that repeats 3 times, each with one slight variation. I’d recommend instead putting the three Color3 values into a table and, within the loop, adding a for i, v in pairs() within, to loop over that one block for every value in the table.
while true do
for i,v in pairs(Table) do -- Assuming the three Color3 values have been put into a table named "Table"
-- The construction of the table's up to you, to help you learn about this sort of thing.
-- Also note the variable "v" mentioned.
light.Brightness = 40
light.Color = v -- "v" represents the entry in the table currently being iterated over.
-- Provided the table has been constructed correctly, this should be one of the Color3 values.
wait(timechange)
light.Brightness = 0
wait(timechange)
end
end
Something like this should, in theory, work. I’d recommend reading about pairs and ipairs for more about this sort of thing.
In addition to this, I’ve noticed you’re toggling the light on and off via its Brightness property. Instead of this, I’d recommend modifying the code to instead modify the Enabled property.
It is also bad practice to have unconditioned loops in your code. I would replace the while loop with RunService’s heartbeat.
So it would look something like this:
local RunService = game:GetService("RunService")
local Table = [Color3.new(1, 0.333333, 0), Color3.fromRGB(0, 255, 255), Color3.new(0.666667, 0.333333, 1)]
RunService.Heartbeat:Connect(function()
for _,v in pairs(Table) do -- Assuming the three Color3 values have been put into a table named "Table"
-- The construction of the table's up to you, to help you learn about this sort of thing.
-- Also note the variable "v" mentioned.
light.Brightness = 40
light.Color = v -- "v" represents the entry in the table currently being iterated over.
-- Provided the table has been constructed correctly, this should be one of the Color3 values.
wait(timechange)
light.Brightness = 0
wait(timechange)
end
end
However wait is not a good idea to use for 2 reasons:
It is depreciated
It pauses the function making it unfeasable to use for anything else
We can get around this by storing the last time the light changed and comparing it to the current time, then only running the code whenever the correct amount of time has elapsed.
local RunService = game:GetService("RunService")
local Table = [Color3.new(1, 0.333333, 0), Color3.fromRGB(0, 255, 255), Color3.new(0.666667, 0.333333, 1)]
local timeChange = 1 -- In seconds
local lastTime = tick()
local index = 1
RunService.Heartbeat:Connect(function()
if lastTime + timeChange >= tick() then
if light.Enabled then
light.Enabled = false
else
light.Color = Table[index]
light.Enabled = true
index += 1
if index > #Table then
index = 1
end
end
end
end