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