Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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?

1 comment

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 = {...}

1 answer

✓ Accepted answer

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

1 comment

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;

This discussion is preserved as it was. New answers, comments, and votes are closed.