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

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

Asked by
gskw 1046 Moderation Voter
10 years ago

I want to create a customizedprint() 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.

0
As said, in that LUA book, some things may not be able to do in Roblox. RolandStudio 115 — 10y
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. BlueTaslem 18071 — 10y

1 answer

Log in to vote
2
Answered by 10 years ago

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.

Ad

Answer this question