New to gmod lua

I have used lua on other games such as roblox, but every time has its own play on things so i am very very confused.

Im using luapad to do this.


x = 2
while true do
x = x-1
	timer.Create( "timer", 10, 1, function()
	if x == 1 then
		msg("x = 1")
	elseif x == 0 then
		msg("x = 0")
	else
		("x = )... x)
end
end)
end

All i could find about a “wait” function was timer.Create and i am very confused. Also this crashed my gmod client, so I am wondering if this caused an infinite loop despite my attempt on a wait function.
If someone could tell me what the hell I should use to “wait()” or “sleep()” so it doesnt infinite loop it would be much appreciated
PS I want this to repeat every 10 seconds, so that is why I am using while true do.
Thanks!
(also I wrote this from scratch so there may be some other errors, but please focus on my question. Thanks :downs: )

[editline]23rd December 2012[/editline]


x = 2
while true do
    x = x-1
    timer.Simple(10, msg)
    if x == 1 then
        msg("x = 1")
    elseif x == 0 then
        msg("x = 0")
    else
        msg("x = not 1 or 0")
end
end

Came up with that after doing a bit more research ( I figured timer.Simple would be more simpler than timer.Create. but I’m not exactly sure on how to use it and what arguments to use.

Why would you put a timer within a loop? That creates a timer each time it loops around.

“while true do” creates an infinite loop no matter what. Are you trying to loop until x is -1?

[editline]23rd December 2012[/editline]

Here is info on loops.

A timer creates an event in the future, it doesn’t stop the execution of the code.

You only have to create the timer once for it to repeat itself, if you make it properly.

No I’m simply trying to create a loop that never stops and waits 10 seconds between each loop.
If I wanted to do it until it reaches 10 i can do
(this is a random number gen for 0 to 100)



for i=1, 10 do
    x = 0
    x = math.random(0,100)
       Msg(x.."-----"..i.."
")
end


But what if I want it to go forever. Does loops like “while true do” cause errors? What is the preferred infinite loop command? (and a way to stop it from crashing D:)


timer.Create( "some unique name", 10, 0, function()
	//your stuff here
end )