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

Creating a function with an unknown number of arguments; arg == nil?

I want to create a customized print() function that would print the arguments to the console and then also put them in a StringValue. I read an article on how to do that. My code is as the following:

function printConsole(...)
	coroutine.wrap(function(arg)
		local toPrint=""
		print(arg)
		for _,v in pairs(arg) do
			toPrint=toPrint..tostring(v)
		end
		print(toPrint)
		workspace.ToPrint.Value=workspace.ToPrint.Value..toPrint.."\n"
	end)(arg)
end
print("Hi", "there!")

At the for loop it breaks because arg is nil, not a table. At print(arg) it says: "Hi" so it thinks arg is a string.

2 comments

0

As said, in that LUA book, some things may not be able to do in Roblox.

0

Actually, no. ROBLOX does not modify that sort of implementation, that would be insane. The examples in PIL don't work in real, standalone Lua installations, either, which confused me the other day.

1 answer

✓ Accepted answer

There is no need for coroutine and you pass a nil value anyway. Here:

function printConsole(...)
    local arg = {...}
    local toPrint=""
    print(arg)
    for _,v in pairs(arg) do
        toPrint=toPrint..tostring(v)
    end
    print(toPrint)
    workspace.ToPrint.Value=workspace.ToPrint.Value..toPrint.."\n"
end
print("Hi", "there!")

FYI, roblox allows you to redefine 'print' function itself, so you can do that.

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