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.
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")
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.
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.
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]
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?