Jump to content

Save a Table to Account Data


MrCode

Recommended Posts

I need to save a table with some values to a player's account so I can then later on extract those values from the target account.

For example: 

local values = getAccountData ( getPlayerAccount ( source ), "SavedTable" )
print: values[1] being "MrCode" or values[2] being "Deez Nutz 1234"


The test function ( saveTableToAccount ) doesn't seem to save the table to the account at all, there are no errors either.
So how do we save the table?

function saveTableToAccount( source )

	-- values what we want to save
	local playerName = getPlayerName ( source )
	local playerInfo = "Deez Nutz 1234"

	-- set what we have as a table value in account data
	local tableToSave = { playerName, playerInfo }
	setAccountData( getPlayerAccount ( source ), "SavedTable", tableToSave )

	-- what we saved
	outputChatBox( "saveTableToAccount - Player Name: (" .. playerName .. ") Infomation: (" .. playerInfo .. ")" )
end
addCommandHandler( "save", saveTableToAccount )



-- Management / Utility
-- To check what we've saved using 'saveTableToAccount' test function
-- This function returns a table containing all the user data for the account provided

function printAllData ( thePlayer )
    local playerAccount = getPlayerAccount( thePlayer ) -- get his account
    if ( playerAccount ) then -- if we got the account then
        local data = getAllAccountData( playerAccount ) -- get data
        count = 0
        for _ in pairs(data) do count = count + 1 end -- get the count
        outputChatBox ( "table holds " .. count .. " entries" ) -- output number of rows
        if ( data ) then
            for k,v in pairs ( data ) do
                outputChatBox(k..": "..v) -- print the key and value of each entry of data
            end
        end
    end
end
addCommandHandler( "getall", printAllData ) -- add a command handler for command 'getall'

 

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...