Problem with while true loops in threads in roblox

60 Views Asked by At

So the problem i have is when I resume a thread with a while true loop inside and I do it again, it does the double. (I don't want that)

Can someone tell me how do I stop something like this from happening.

I have tried so far this:

-- This is a ModuleScript btw
-- Ignore the code with

local makedisaster = {}
local LightsEnabledMain = false


local function runlights(color,enabled,timereal)
    local LightRep = workspace.controlcentre.WALLS.chamber.lightsbeta
    local TwS = game:GetService("TweenService")

    local function doliht(numbah,timedelay)
        for _,v in pairs(LightRep:FindFirstChild(numbah):GetChildren()) do
            local function dolight()
                local tween = TwS:Create(v,TweenInfo.new(timedelay/2,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut),{Color = color})
                tween:Play()
                tween.Completed:Wait()
                task.wait(timedelay)
                local tween2 = TwS:Create(v,TweenInfo.new(timedelay/2,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut),{Color = Color3.new(0,0,0)})
                tween2:Play()
                tween2.Completed:Wait()
                task.wait(timedelay/3)
            end
            local thread = coroutine.create(dolight)
            coroutine.resume(thread)
        end
    end

    while true do -- This is that while loop.
        if LightsEnabledMain == true then
            for numbarcurrent,v in pairs(LightRep:GetChildren()) do
                local thread = coroutine.create(doliht)
                coroutine.resume(thread,"L"..numbarcurrent,timereal)
                wait(1.5)
            end

        else
            break
        end
        task.wait()
    end
end


local runlightthread = coroutine.create(runlights)

function makedisaster.reallights(colorahh,enabledreal,timemorereal)
    LightsEnabledMain = enabledreal
    if coroutine.status(runlightthread) ~= "dead" then
        print("NormalOperation")
        if LightsEnabledMain == true and coroutine.status(runlightthread) == "suspended" then
            if coroutine.status(runlightthread) ~= "running" then
                print(coroutine.status(runlightthread))
                coroutine.resume(runlightthread,colorahh,enabledreal,timemorereal)
                print("NormalResume")
            end
        elseif LightsEnabledMain == false and coroutine.status(runlightthread) == "running" then
            print("Closing")
            coroutine.close(runlightthread)
        elseif LightsEnabledMain == true and coroutine.status(runlightthread) == "running" then
            print("Wrapped")
            coroutine.close(runlightthread)
            runlightthread = coroutine.create(runlightthread)
            coroutine.resume(runlightthread,colorahh,enabledreal,timemorereal)
        end
    else
        if LightsEnabledMain == true then
            print("Recreate")
            runlightthread = coroutine.create(runlights)
        end
    end
end


return makedisaster

The while true loop basically runs after the thread died i think and I have tried breaking it with a bool variable

0

There are 0 best solutions below