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
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.