Jump to content

outputChatBox a table with accounts


maffius

Recommended Posts

addCommandHandler("myAccounts",
	function (player, cmd)
		local serial = getPlayerSerial(player)
		local accounts = getAccountsBySerial(serial)
		outputChatBox("You have " .. #accounts .. " accounts with these logins: ".. table.concat(accounts, ", "), player)
	end)

And it gives error:
 

invalid value (userdata) at index 1 in table for 'concat'


How can I make this table to string?

Edited by maffius
Link to comment

The table returns accounts, why do you want to output them as string? Even if you make them string they would look like "Userdata: xxxxx" which is no use at all. Do you want to output the account names because that's a different thing.

Link to comment
1 hour ago, pa3ck said:

Do you want to output the account names because that's a different thing.

If that's what you want to do, you'll have to create an intermediate table with the account names as strings on it, and pass that to the table.concat function. table.concat expects a table with strings on its indexes, but does not convert any data in it to a string itself. This code should comply with your needs:

addCommandHandler("myAccounts",
    function (player, cmd)
        local accounts = getAccountsBySerial(getPlayerSerial(player))
        local accountNames = {}
        local accountNumber = 0
        for _, acc in pairs(accounts) do
            accountNumber = accountNumber + 1 -- Why use the table length operator when you iterate over its elements and can do this? This seems more efficient
            accountNames[accountNumber] = getAccountName(acc)
        end
        outputChatBox("You have " .. accountNumber .. " accounts with these logins: ".. table.concat(accountNames, ", "), player)
    end
)

If you have any doubt of why or how does that work you can add a reply to this post, so someone can help you out :)

Edited by AlexTMjugador
Optimizations & typos
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...