How to I make this less laggy

Hello, this flicering light script gives you a lag spike if you get close to it how do I fix this? while true do wait (1) script.Parent.Light.PointLight.Enabled = false script.Parent.Light.Transparency = 0.5 wait (0.06) script.Parent.Light.PointLight.Enabled = true script.Parent.Light.Transparency = 0 wait (0.05) script.Parent.Light.PointLight.Enabled = false script.Parent.Light.Transparency = 0.5 wait (0.08) script.Parent.Light.PointLight.Enabled = true script.Parent.Light.Transparency = 0 wait (0.07) script.Parent.Light.PointLight.Enabled = false script.Parent.Light.Transparency = 0.5 wait (1) script.Parent.Light.PointLight.Enabled = true script.Parent.Light.Transparency = 0 wait (3) script.Parent.Light.PointLight.Enabled = false script.Parent.Light.Transparency = 0.5 wait (0.02) script.Parent.Light.PointLight.Enabled = true script.Parent.Light.Transparency = 0 wait (0.07) script.Parent.Light.PointLight.Enabled = false script.Parent.Light.Transparency = 0.5 wait (0.05) script.Parent.Light.PointLight.Enabled = true script.Parent.Light.Transparency = 0 wait (0.02) script.Parent.Light.PointLight.Enabled = false script.Parent.Light.Transparency = 0.5 wait (1) script.Parent.Light.PointLight.Enabled = true script.Parent.Light.Transparency = 0 end

1 Like

Please properly format your code using ``` and pasting your code there.

Maybe because there is so many things trying to happen at once. Try using loops

local Light = script.Parent.Light
local PointLight = Light.PointLight

local WaitTime = 0.15

while true do
    PointLight.Enabled = not PointLight.Enabled
    Light.Transparency = math.abs(Light.Transparency - 0.5)

    task.wait(WaitTime)
end

not sure how you would want the wait time so I made it 0.15 by default
you can change WaitTime if you need to

if you want different wait times like you have, you can use a table with all the wait times

the previous code if you had multiple wait times
local Light = script.Parent.Light
local PointLight = Light.PointLight

local WaitTimes = {
    0.1,
    0.15,
    0.05,
    0.5,
    0.3
}

while true do task.wait()
    for _, WaitTime in ipairs(WaitTimes) do
        PointLight.Enabled = not PointLight.Enabled
        Light.Transparency = math.abs(Light.Transparency - 0.5)

        task.wait(WaitTime)
    end
end

you can always change what wait times are in the table

2 Likes

you can also put waits in the while condition!

local Light = script.Parent.Light
local PointLight = Light.PointLight

while task.wait() do
    PointLight.Enabled = not PointLight.Enabled
    Light.Transparency = math.abs(Light.Transparency - 0.5)
end

I didn’t do that for two reasons

  1. this code needs to run first before we yield at all
PointLight.Enabled = not PointLight.Enabled
Light.Transparency = math.abs(Light.Transparency - 0.5)
  1. I personally like yielding inside the loop
1 Like