Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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?

1 comment

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.

1 answer

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

1 comment

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?

This discussion is preserved as it was. New answers, comments, and votes are closed.