Script not repeating like supposed to

So I have this part of my script that is supposed to repeat after 1 second, only if the val2 is true, but it doesn’t, any help?

if strobesync then

while wait(1) do

if val2.Value == true then

Flash()

repeat wait(1) until script.Parent.Parent.Parent.TurnedOn == false

end

end

end
2 Likes

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

2 Likes

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.

1 Like

I don’t know why you even have a repeat loop when you’re not even setting up the possibility for the boolean to turn false.

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

No one here seems to understand that you can set the condition of a while-loop.

while turnedOn.Value == true do
	print("turned value true")
	Flash()
	print("Played flash")
	wait(1)
end
print("stopped")

This doesn’t work, doesn’t do the function, doesn’t even print.

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!