Hi. I have a code and it has a while loop in it. I want code to do something in while loop only once. Here is an example;
while true do doOnceThing print('Script started.') end --Some other code end
How can i do it?
EDIT: I know I can put print() outside loop but it's just an example I can't do it on my own code.
This can be solved with an if statement that is only true if the condition is met. Here is an example
local doOnceThing = 1 while true do if doOnceThing == 1 then --insert your code here doOnceThing = doOnceThing + 1 --this will make it never run again end --Some other code end
This is a bit crude since it will constantly check if doOnceThing is equal to 1, but it will solve your current problem.
You can just do that thing before the while loop, like so:
print('Script started.') while true do -- Some other code end
But if you specifically want the thing to be done in the while loop you can simply add a boolean to the code and then make something like this:
local didOnceThing = false while true do if didOnceThing == false then print('Script started.') didOnceThing = true end -- Some other code end
Though it's better to use the first way.