title. cant figure out another way to constantly check something.
I do not understand the situation. However, yes, while loops or for loops do lag the game. Why, well this deals with Big O. Basically, code takes up space and time. To run a for loop you in behind the scenes for loops instantiate variables, then constantly check to see the variables being updated, then it runs the code in the body, and keeps doing this. This is why it takes time and space. Because you are putting in memory the local variable in a for loop then the code keeps checking if the variable is updating. While True has no condition just that it is true. So, this lags the game because it is constantly checking for a condition that will always be true. This takes up space and time. These are behind the scenes things that happen.
To constantly check something I would do a While True then inside have a conditional and a break if that condition is met. It really depends on the situation. I would advise if you can find a way not to use while loops or for loops that way is better because loops do take up space and time. Do not use too many for loops and while loop because simple code is always better than complex ones.
Instead of using:
while true do print("doing this lags the game") end
Try using:
while wait() do print("no lag") end
If you put "wait()" instead of "true" in the statement, it allows the game to have a 1-tick rest period between each time the loop is run, reducing lag greatly. You will also get to avoid the "Execution time exhausted" error.
yes, they do lag the game. while wait() do or while true do wait() also do lag the game, though if you use 1 or 2 inside of your game, you shouldn't experience major lag issues.