kieran Posted January 24, 2018 Share Posted January 24, 2018 (edited) So I have account data outputting to chat, as a test, but I want to add it to a table as I am trying to make a system where all accounts with the players current serial merge data into one account, the only problem is I want to insert it into a table like this: data = {accountName, dataName, dataValue} Here's my current code. local serial = getPlayerSerial( source ) local oldAccounts = getAccountsBySerial( serial ) if not oldAccounts == false then outputChatBox("You have an account and your data will now be transferred to the new one.", source, 50, 255, 200) dataSafe = {} --Empty table to save data for i=1,#oldAccounts do accounts = oldAccounts[i][1] local data = getAllAccountData( accounts ) -- get data if ( data ) then for k,v in pairs ( data ) do table.insert(dataSafe, oldAccounts, k, v) end end end for i=1,#dataSafe do local name, k, v = dataSafe[i][1],dataSafe[i][2],dataSafe[i][3]--x = key 1, y = key 2, z = key 3 outputChatBox(""..name..k..": "..v) -- print the key and value of each entry of data end end I came here asking as I am unsure about table.insert, I read a little online and it just confused me, hoping someone can help explain it. I've got the error: Attempt to index field '?' a userdata value <<< Line 7 If I do accounts = oldAccounts then I get error wrong number of arguments to 'insert' Thanks a lot for any help Edited January 24, 2018 by kieran Updated code, new problem Link to comment
pa3ck Posted January 25, 2018 Share Posted January 25, 2018 I never used getAccountsBySerial myself, but the wiki says it returns a table of accounts, so I assume you get back something like this: { Account1, Account2, Account3} So, what if you try: accounts = oldAccounts[i] -- INSTEAD OF accounts = oldAccounts[i][1] Link to comment
kieran Posted January 25, 2018 Author Share Posted January 25, 2018 (edited) 6 hours ago, pa3ck said: I never used getAccountsBySerial myself, but the wiki says it returns a table of accounts, so I assume you get back something like this: { Account1, Account2, Account3} So, what if you try: accounts = oldAccounts[i] -- INSTEAD OF accounts = oldAccounts[i][1] Believe it or not I sorted this problem last night, but.... I still have big problems with table.insert Here is my updated code, I can hopefully figure it all out if you or someone else helps me understand where I am going wrong with inserting into the table... local accountSafe = {} local serial = getPlayerSerial( source ) local oldAccounts = getAccountsBySerial( serial ) --So this returns a table, so in my mind, this will then BECOME a table... if not oldAccounts == false then --If they have old accounts (I will add checks for current account later) outputChatBox("You have an account and your data will now be transferred to the new one.", source, 50, 255, 200) --Tell them dataSafe = {} --Here is my "dataSafe" where I will use table.insert to add each index of, the account name, the data, the value local name --To store name for i=1,#oldAccounts do --For each account accounts = oldAccounts[i] --This is how I get the data from table name = getAccountName ( accounts ) --I get the name with "accounts" and it's the players name local data = getAllAccountData( accounts ) --I then get all the players data if ( data ) then --If they have data for k,v in pairs ( data ) do --Data returns tuple (Name, Value) table.insert(dataSafe, tostring(name)..","..tostring(k)..","..tostring(v)) --Here is the issue, it adds "nil" end end end for i=1,#dataSafe do --Outside my for to cycle through accounts is where I will merge data, for now it prints the table name, k, v = dataSafe[i][1],dataSafe[i][2],dataSafe[i][3] --So it does index them, BUT, it indexes as nil... outputChatBox(tostring(name)..""..tostring(k)..""..tostring(v), source) --As you can see when it prints end end All you'd have to do to test this and see what I mean is add an onPlayerJoin handler... I have tried adding indexes multiple ways, separated by commas. inserted with the index as a second argument as you'll see here, and even tried adding them together the same way you would if you were to output to chat, nothing but "nilnilnil" Just so you know, the result I am trying to get is: dataSafe = { {Account1,Name1,Value1}, {Account1,Name2,Value2}, {Account2,Name1,Value1}, --Each account gets the account name, then the account data name and then data value. {Account2,Name2,Value2}, {Account3,Name1,Value1}, {Account3,Name2,Value2} --Just keeps adding lines like above, obviously table is hidden, but this is to show what I want to happen in the background... } Here is a quick link to lua manual where table.insert is on page, I just don't understand the lua manual that clearly no matter how many times I read it http://www.lua.org/manual/5.2/manual.html#6.5 I have no idea how to use it really and I am just blindly trying different things until it works... On the bright side, I'm slowly getting there, it's just I could really do with an in depth view to table.insert.... Edited January 25, 2018 by kieran Link to comment
Moderators IIYAMA Posted January 28, 2018 Moderators Share Posted January 28, 2018 local serial = getPlayerSerial( source ) local accounts = getAccountsBySerial( serial ) if accounts and #accounts > 1 then outputChatBox("You have an account and your data will now be transferred to the new one.", source, 50, 255, 200) local dataSafe = {} --Empty table to save data for i=1, #accounts do local account = accounts[i] local allAccountData = getAllAccountData( account ) local accountName = getAccountName(account) for key, value in pairs ( allAccountData ) do dataSafe[#dataSafe + 1] = {accountName, key, value} end end for i=1, #dataSafe do --Outside my for to cycle through accounts is where I will merge data, for now it prints the table local name, k, v = dataSafe[i][1],dataSafe[i][2],dataSafe[i][3] --So it does index them, BUT, it indexes as nil... outputChatBox(tostring(name)..""..tostring(k)..""..tostring(v), source) --As you can see when it prints end end There is also this function: (if you are interested in moving/replace data. Never used it, so I do not know how it will merge) https://wiki.multitheftauto.com/wiki/CopyAccountData 1 Link to comment
kieran Posted January 29, 2018 Author Share Posted January 29, 2018 Thanks so much @IIYAMA, was driving me crazy, like how you even went to the efforts of checking number of accounts! The sole purpose of this part is if I ever upload it to the community, so servers using a GUI system can switch to this and players (hopefully) won't lose any data. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now