Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

unknown global arg when making a function with a variable amount of arguments?

Asked by 5 years ago

a perfect example

function Foo(...)
    for k, v in pairs(arg) do
        print(v)
    end
end

Foo("Hello", "World!", "", "How", "Are", "You", "Today")

for those who dont know, you can make a function with variable arguments via "...", and you access all the arguments as a table via "arg", but roblox keeps saying "W001: Unknown global "arg"", help?

0
Interesting. Running it on Lua Interpreter, it works perfectly fine. I guess in Roblox Studio, you would have to declare it as it showed an "Unknown global variable" error. Declare args by doing `local args = {...}` Zafirua 1348 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

Hi fan,

You need to put the infinite arguments inside of a table. So, basically, create a new table and place them in there and then you can loop through them. Because initially, they're just a number of values separated by commas. So, do something like this:

function variadic(...)
    local args = {...};

    for _, arg in next, args do
        -- Blah
    end 
end

Hope I helped and have a wonderful day/night.

Thanks,

Best regards,

~~ KingLoneCat

0
After a little bit of researching, I found out that OP's code *does* work in Lua 5.2, just not in Lua 5.1; Zafirua 1348 — 5y
Ad

Answer this question