Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is this "while" loop running more than once?

Asked by 5 years ago

I made a backpack that is welded to the player and when a GUI button is clicked, it beeps and blinks a light. Everything runs as expected, I have events to trigger everything from the client to the server, except a part of the code runs more than once at a time.

Here is my code:

local RE = script.RemoteEvent
local light = script.Parent
local active = false
local Ebeep = script.Parent.EBeep
local NBeep = script.Parent.beep

local normalColor = BrickColor.new("Neon orange")
local emergencyColor = BrickColor.new("Really red")
local offColor = BrickColor.new("Smoky grey")

RE.OnServerEvent:Connect(function(plr)
    if active then
        active = false
    else
        active = true
    end

    while active do
        print("Active")
        light.BrickColor = emergencyColor
        light.Material = Enum.Material.Neon
        light.Transparency = 0
        Ebeep:Play()
        wait(1)
        light.BrickColor = offColor
        light.Material = Enum.Material.Glass
        light.Transparency = 0.1
        wait(1)
    end

    while not active do
        print("Not Active")
        light.BrickColor = normalColor
        light.Material = Enum.Material.Neon
        light.Transparency = 0.1
        NBeep:Play()
        wait(5)
    end
end)

It all runs smoothly except for this block which for some reason runs more than once at a time:

while not active do
    print("Not Active")
    light.BrickColor = normalColor
    light.Material = Enum.Material.Neon
    light.Transparency = 0.1
    NBeep:Play()
    wait(5)
end

Anyone know why this is happening?

0
The whole point of a while loop is to execute while the condition is met. Since your condition looks for the active variable's value to be falsey, the loop will execute while that condition is met. User#24403 69 — 5y

1 answer

Log in to vote
0
Answered by
LuaDLL 253 Moderation Voter
5 years ago

You have it check if active is true, and if it is then it makes it false, but right after it checks if its false, which you just set it as false so it will change it back to true

local RE = script.RemoteEvent
local light = script.Parent
local active = false
local Ebeep = script.Parent.EBeep
local NBeep = script.Parent.beep

local normalColor = BrickColor.new("Neon orange")
local emergencyColor = BrickColor.new("Really red")
local offColor = BrickColor.new("Smoky grey")

RE.OnServerEvent:Connect(function(plr)
   local AlreadyChecked = false
    if active and not AlreadyChecked then
        active = false
        AlreadyChecked = true
    elseif not active and not AlreadyChecked then
        active = true
        AlreadyChecked = true
    end

    while active do
        print("Active")
        light.BrickColor = emergencyColor
        light.Material = Enum.Material.Neon
        light.Transparency = 0
        Ebeep:Play()
        wait(1)
        light.BrickColor = offColor
        light.Material = Enum.Material.Glass
        light.Transparency = 0.1
        wait(1)
    end

    while not active do
        print("Not Active")
        light.BrickColor = normalColor
        light.Material = Enum.Material.Neon
        light.Transparency = 0.1
        NBeep:Play()
        wait(5)
    end
end)

I don't know if that was the problem but I tried to help you out a little bit here

0
Thanks for trying to help, but that did not work. I did manage to find out that it has something to do with the "active" loop because after commenting it out, the "not active" loop works as intended. Anyone know what is causing this? BaconMan1455 4 — 5y
Ad

Answer this question