• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Small question about loops.

Animera

* * * * *
Joined
Dec 9, 2008
Messages
2,432
Solutions
5
Reaction score
603
Location
ANIMERA.ONLINE
Hello, i am using TFS 1.2 and i have a small question about loops.

When i am scripting and use loops like "while XXX do"
my server freezes(99% cpu use?) Everyone gets kicked etc.

What does it make it so that whenever i try to loop through 'while' it freezes?
is it because of 99999 threads every 000.1 milisecond or w/e?

Or what is the correct way of executing/using loops as "while"?

Animera
 
Solution
if it was exactly like that then it wouldn't run forever
test your code here Lua: demo
add a print(chicken) inside the while loop and youll see it only runs up til 9
it's hard to tell you when you just give pseudocode instead of the actual code you're trying to use
Most likely your condition is always true so it continually spams your code. Show us an example of how you are using it
 
while loops are dangerous if you don't know what you're doing since they check for a condition continuously to execute, rather than for loops being a set amount of runs to loop
it's hard to tell you what you're doing wrong if you don't show an example of what you mean
your condition is most likely true forever which stops all execution to run the while loop since it never stops because your while condition is always true
 
Most likely your condition is always true so it continually spams your code. Show us an example of how you are using it

while loops are dangerous if you don't know what you're doing since they check for a condition continuously to execute, rather than for loops being a set amount of runs to loop
it's hard to tell you what you're doing wrong if you don't show an example of what you mean
your condition is most likely true forever which stops all execution to run the while loop since it never stops because your while condition is always true
Code:
chicken = 1
while chicken < 10 do
chicken = chicken + 1
end

Well i do not have an actually an example. But it was similar like that.


if i use loops like
Code:
for i = 1, #chickennuggets
or similar it works fine..
 
that while loop is fine, the condition eventually becomes false as soon as chicken becomes 10, so there's no way it was like that
 
that while loop is fine, the condition eventually becomes false as soon as chicken becomes 10, so there's no way it was like that

Believe me it was exactly like that, except i used a other variable then chicken or chickennuggets..
I only have that problem with loops by "while/do" or "repeat/until"..

which is not troublesome as i could use a addEvent in a addEvent untill i have what i want..
But i think while is cleaner.. as i want to know more about LUA (and that colandus shown loop examples with those functions).
Made me curious what i did/do wrong..
 
if it was exactly like that then it wouldn't run forever
test your code here Lua: demo
add a print(chicken) inside the while loop and youll see it only runs up til 9
it's hard to tell you when you just give pseudocode instead of the actual code you're trying to use
 
Solution
In while loop it's advised to break when it gets to the condition you have specified else it might loop forever
here's example using for and while you can test it with lua demo as @Static_ said
Using While loop
Lua:
chicken = 1
while chicken < 10 do
if chicken == 9 then
   chicken = chicken - 1
   break --if you comment this line it will loop forever
else
   chicken = chicken + 1
end
print(chicken)
end
Using For loop
Lua:
chicken = 1
for i = 1,10 do
if chicken == 9 then
   chicken = chicken - 1
else
   chicken = chicken + 1
end
print(chicken)
end
 
In while loop it's advised to break when it gets to the condition you have specified else it might loop forever
you don't need to break after you meet the condition, if you meet the condition and it turns to false then the execution stops
unless you're looking to break because of another condition inside of it that isn't the while condition
 
In while loop it's advised to break when it gets to the condition you have specified else it might loop forever
here's example using for and while you can test it with lua demo as @Static_ said
Using While loop
Lua:
chicken = 1
while chicken < 10 do
if chicken == 9 then
   chicken = chicken - 1
   break --if you comment this line it will loop forever
else
   chicken = chicken + 1
end
print(chicken)
end
Using For loop
Lua:
chicken = 1
for i = 1,10 do
if chicken == 9 then
   chicken = chicken - 1
else
   chicken = chicken + 1
end
print(chicken)
end
ye if it gets stuck in while loop something bad might happen like chicken gets eaten by wolf if condition not met right?
 
Back
Top