Don’t ever put wait() in the while condition. A while loop continually checks the boolean argument you give it (wait(1)) in this case to decide if it should repeat. Wait(1) isn’t designed to return a boolean. Use while true do wait(1).
Your issue is in the check script.Parent.Parent.Parent.TurnedOn == false. TurnedOn is an instance and not a boolean, so a comparison to false will never be true.
I’m assuming TurnedOn is some sort of value object, so indexing it with TurnedOn.Value should fix your issue.
local turnedOn = script.Parent.Parent.Parent.TurnedOn -- variable so code isn't too polluted
if strobesync then
while true do
if val2.Value then
Flash()
if turnedOn.Value then -- is your value true?
turnedOn.Changed:Wait() -- wait till it switches to false
end
else
wait(1) -- your wait from your code
end
end
end
Here is a refactor of the code avoiding changing the logic too much, since I don’t know the actual context. While your only real issue is the lack of a .Value check, it never hurts to also make things more idiomatic. Avoid loops where events can be used, use variables where your lines are getting long, and so forth.
So I made it a little bit different, and did this, but now its not waiting the 1 second?
while true do
if turnedOn.Value == true then
print(“turned value true”)
Flash()
print(“Played flash”)
if turnedOn.Value == false then
turnedOn.Changed:Wait()
print(“changed”)
end
else
wait(1)
print(“Waited 1”)
end
end
This script only waits 1 second, if turnedOn is false. If you want to make it wait 1 second when turnedOn is true it should look like this:
while true do
if turnedOn.Value == true then -- turned on
print("turned value true")
Flash()
print("Played flash")
wait(1)
else -- not turned on
break -- stop repeating
end
end
I’ve got a spooky idea, if you’d like to hear it @Pooglies
get rid of the while loop entirely
If you only want the strobe light to light up when a value == true, then all you really need to do is listen for when the value is changed. This can be easily done with a .Changed signal. Upon getting the signal, your function could check the value of val2, and if it’s true, then use a repeat loop to continue flashing until the value has changed.
I.E
local StrobeActive = false -- this is here incase you want to add other things that rely on strobe being active.
turnedOn.Changed:connect(function()
if turnedOn.Value == true then
StrobeActive = true
repeat
wait(1)
Flash()
until StrobeActive == false
-- Starts the loop up until the variable "StrobeActive" == false
else
StrobeActive = false
-- Shuts down any previous loop thats going on
end
end)
I believe this should solve your predicament, feel free to let me know if it doesn’t!