# Lua "arg" variable not working (SOLVED)

**URL:** <https://forum.defold.com/t/lua-arg-variable-not-working-solved/22271>\
**Category:** Bugs\
**Created:** [June 15, 2018, 8:05pm UTC](https://forum.defold.com/t/lua-arg-variable-not-working-solved/22271 "2018-06-15T20:05:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![leland.kwong](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/leland.kwong/32/8763_2.png) [@leland.kwong](https://forum.defold.com/u/leland.kwong)\
**Post date:** [June 15, 2018, 8:05pm UTC](https://forum.defold.com/t/lua-arg-variable-not-working-solved/22271/1 "2018-06-15T20:05:44Z")

</div>

Lua has a global variable `arg` available for variable arguments in functions. For some reason it is set to `nil`?

Here is a clean example for how the expected behavior is.  
[https://repl.it/repls/DarkblueOverjoyedCompilerbug](https://repl.it/repls/DarkblueOverjoyedCompilerbug)

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [June 15, 2018, 8:21pm UTC](https://forum.defold.com/t/lua-arg-variable-not-working-solved/22271/2 "2018-06-15T20:21:12Z")

</div>

We use LuaJIT for all platforms but iOS 64 and HTML5. The [LuaJIT FAQ](http://luajit.org/faq.html) reads:

“Q: Why do I get this error: “attempt to index global ‘arg’ (a nil value)”?  
Q: My vararg functions fail after switching to LuaJIT!  
LuaJIT is compatible to the Lua 5.1 language standard. It doesn’t support the implicit arg parameter for old-style vararg functions from Lua 5.0.  
Please convert your code to the Lua 5.1 vararg syntax.”

You can get the arguments in two ways:

```
function foo(...)
   local args = {...}
   print(args[1])
   print(select(1, ...))
end

```

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [June 15, 2018, 8:35pm UTC](https://forum.defold.com/t/lua-arg-variable-not-working-solved/22271/3 "2018-06-15T20:35:06Z")

</div>

I might be missing something, but I cannot seem to use `arg`in my local Lua 5.2. Do I need to set things up first?

```lua
Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio
> function test(...)
>> print("num args", #arg)
>> end
> test("hello")
stdin:2: attempt to get length of global 'arg' (a nil value)
stack traceback:
	stdin:2: in function 'test'
	stdin:1: in main chunk
	[C]: in ?

```

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [June 15, 2018, 9:21pm UTC](https://forum.defold.com/t/lua-arg-variable-not-working-solved/22271/4 "2018-06-15T21:21:13Z")

</div>

> [@Mathias\_Westerdahl](#):
>
> I cannot seem to use argin my local Lua 5.2. Do I need to set things up first?

It’s probably removed in 5.2 and exists for backwards compatibility in 5.1

---

<div class="post-metadata">

**Author:** ![leland.kwong](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/leland.kwong/32/8763_2.png) [@leland.kwong](https://forum.defold.com/u/leland.kwong)\
**Post date:** [June 15, 2018, 10:52pm UTC](https://forum.defold.com/t/lua-arg-variable-not-working-solved/22271/5 "2018-06-15T22:52:19Z")

</div>

That explains everything. Thanks @britzl!
