While something == true not working when set back to true

If I have a variable that is true, then the print works. And when it is false then it stops printing. I am just wondering why it doesn’t start printing again when the variable is set back to true and if there are any workarounds to this.

local something = true

spawn(function()
	while something == true do
		wait()
		print("this prints")
	end
end)

wait(5)
something = false
print("false")
wait(5)
something = true
print("true")

The spawn is used for demonstration just so that the while loop works alongside the stuff below it.

2 Likes

That’s because the loop is broken out of once ‘something’ is set to false, you need to re-execute the loop.

2 Likes

To elaborate on @Forummer and to show you the reason:
(And also a better way to do while loops that doesn’t involve not using them)

local something = true

-- Task library on top, or at least it is imo.

task.spawn(function()
	while true do
		-- while true do is better than while function() do since it doesn't need to evaluate a function every loop.
		-- This also fixes your issue of the loop breaking since it will continue to run but not print if it's false.
		task.wait()
		if something == true then
			print("this prints")
		end
	end
end)

task.wait(5)
something = false
print("false")
task.wait(5)
something = true
print("true")
5 Likes

That’s a bad idea. Your loop would basically run the entire game and only print when the var is equal to true. So when it’s not, you just have a loop running for no reason.

That’s also exactly what he wants.

This is simply an explanation for him of the basics on while loops.

And if it wasn’t printing, this loop takes up virtually 0 performance. If anything he could make the wait time longer between loops for even less performance.

3 Likes

30 posts were split to a new topic: While something == true not working when set back to true (Private Discussion)

local something = true

local function RunLoop(param)
	spawn(function()
		while true do
			task.wait()
			if param then
				print("this prints")
			else
				return
			end
		end
	end)
end


RunLoop(something)

task.wait(4)
something = false
print("false")

RunLoop(something)

task.wait(4)
something = false
print("true")


RunLoop(something)

A simple idea that you could use. For me it worked nicely.
[Could be done better prob]

2 Likes

You’ve broken out of the loop and as a result the program isn’t going to revisit it.

2 Likes

Loops cannot be restarted unless you nest it in a loop. Therefore you should utilize a function that executes the loop. Alternatively, you would execute the function on something changing(or in order).

Now the final question is in what application(as in way of applying) do you intend to use it on?

1 Like

You should be careful when doing code as you have done.

You may want to review this RDC presentation about 5 Powerful Code Patterns, specifically looking at Signals and how they can be used.

You may find that designing your code to use signal based programming will improve your experience and make your codebase much clearer.

2 Likes