JAY.ANN Posted November 27, 2022 Share Posted November 27, 2022 (edited) Hello everybody! I'd like to introduce you my own-made systems based on internal.db for small gamemodes or simple servers. I was thinking about how good it'd be if we'd have an abillity to work with account's saved data better and faster only using standard MTA database. I've been looking for an information that could help me to put a lot of data (as player's position, skin, money and so on) in 1 key to internal.db account's data. As for me - this is the best way of using saveAccountData() for non-global servers because it reduces the amount of trash-files in database. I made some researches on that and found out that it doesn't affect the perfomance. Download: https://community.multitheftauto.com/index.php?p=resources&s=details&id=18786 Preview: ------------------------------- ----ACCOUNT ID SYSTEM UTILS---- ------------------------------- function getAccountDatabaseID( account ) if account and not isGuestAccount( account ) then accID = 0 -- Set account ID to default if getAccountData( account, "data" ) and getAccountData( account, "data" ) ~= nil then -- Check out for account's data table local data = fromJSON( getAccountData( account, "data" ) ) -- Get account's data table if data[ "ID" ] == nil then accID = 0 -- Set account ID to default in cause of ID == nil else accID = data[ "ID" ] -- Set account ID to account's data table ID end else accID = 0 -- Set account ID to default in cause of account's data table == nil end return accID -- Return account's ID end end function getFreeDatabaseID() local accounts = getAccounts() missedID = false -- Set an ID to default for operation for i, v in pairs( accounts ) do -- Start a loop for all server accounts if i ~= getAccountDatabaseID( v ) then missedID = i break end end if missedID ~= false then return missedID --[[ ETC: There were 5 accounts. The 3rd one have been deleted. To avoid dissolution of an ID after deleting account we look for the gap and fill it with new account. It means that new account, created afther the 5th, would get 3rd ID of deleted account. --]] else return #accounts + 1 -- There were no gaps in ID row, so we return ID as a count of all accounts + created one. end end ----------------------------------------- ----ACCOUNT ID SYSTEM UTILS ENDS HERE---- ----------------------------------------- -------------------------- ----ACCOUNT DATA TABLE---- -------------------------- function getAccountDataTable( account ) if account and not isGuestAccount( account ) then if getAccountData( account, "data" ) and getAccountData( account, "data" ) ~= nil then local data = fromJSON( getAccountData( account, "data" ) ) -- Get account data table local id = data[ "ID" ] -- Get account ID from data table local position = data[ "Position" ] -- Get account position from data table local skin = data[ "Skin" ] -- Get account skin from data table local money = data[ "Money" ] -- Get account money from data table return id, position, skin, money -- Return all collected data end end end function saveAccountDataTable( account, data ) if account and not isGuestAccount( account ) then setAccountData( account, "data", data ) end end addEvent( "saveAccountDataTable", true ) addEventHandler( "saveAccountDataTable", getRootElement(), saveAccountDataTable ) function getPlayerDataTable( player ) local x, y, z = getElementPosition( player ) -- Get player's position local rotx, roty, rotz = getElementRotation( player ) -- Get player's rotation local interior = getElementInterior( player ) -- Get player's current interior local dimension = getElementDimension( player ) -- Get player's current dimension local skin = getElementModel( player ) -- Get player's skin local money = getPlayerMoney( player ) -- Get player's money local accID = getElementData( player, "ID" ) or 0 -- Get player's account ID ---- local data = toJSON( { [ "ID" ] = accID, -- Prepare account ID to save [ "Position" ] = { x, y, z, rotz, interior, dimension }, -- Prepare player's position to save [ "Skin" ] = skin, -- Prepare skin to save [ "Money" ] = money, -- Prepare money to save } ) ---- return data -- Return all data that we've collected from the player end ------------------------------------ ----ACCOUNT DATA TABLE ENDS HERE---- ------------------------------------ ---------------------------------------- ----IMPLEMENT ALL STUFF THAT WE HAVE---- ---------------------------------------- addEventHandler( "onPlayerLogin", getRootElement(), function( _, account ) if account and not isGuestAccount( account ) then if getAccountData( account, "data" ) and getAccountData( account, "data" ) ~= nil then ---------------------------------------------------- ----Spawn player from saved account's data table---- ---------------------------------------------------- local ID, position, skin, money = getAccountDataTable( account ) spawnPlayer( source, position[ 1 ], position[ 2 ], position[ 3 ], position[ 4 ], skin, position[ 5 ], position[ 6 ] ) fadeCamera( source, true, 3 ) setCameraTarget( source, source ) setPlayerMoney( source, money, true ) setElementData( source, "ID", ID ) -- Apply player's ID outputChatBox( "Welcome back, "..getPlayerName( source ).."!", source, 255, 255, 255, true ) -- Output a welcome message outputChatBox( "Your ID: "..ID, source, 255, 255, 255, true ) -- Output a message to chat for logged player to make a sure that we've got the right ID else ------------------------------------------------------------------------------- ----Spawn player for the first time or in cause of data table reading error---- ------------------------------------------------------------------------------- local ID = getFreeDatabaseID() -- Get free ID for our player to apply it later spawnPlayer( source, 0, 0, 0, 0, 0, 0, 0 ) fadeCamera( source, true, 3 ) setCameraTarget( source, source ) setPlayerMoney( source, 0, true ) setElementData( source, "ID", ID ) -- Apply player's ID outputChatBox( "First time, "..getPlayerName( source ).."? Welcome!", source, 255, 255, 255, true ) -- Output a welcome message outputChatBox( "Your ID: "..ID, source, 255, 255, 255, true ) -- Output a message to chat for logged player to make a sure that we've got the right ID end end end) addEventHandler( "onPlayerLogout", getRootElement(), function( account, _ ) local data = getPlayerDataTable( source ) -- Get player's data to save it saveAccountDataTable( account, data ) -- Save player's data table on log out end) addEventHandler( "onPlayerQuit", getRootElement(), function() local account = getPlayerAccount( source ) local data = getPlayerDataTable( source ) -- Get player's data to save it saveAccountDataTable( account, data ) -- Save player's data table on exit end) P.S: I appreciate any opinion and advice, thank you! You can use this script for any purpose - the code is opened! I've also tried to explain the whole script. I hope it'd help! Download: https://community.multitheftauto.com/index.php?p=resources&s=details&id=18786 Edited November 27, 2022 by JAY.ANN 1 Link to comment
Gemir235 Posted November 27, 2022 Share Posted November 27, 2022 Thank you very much! I've been looking for an ID system for a long time. 1 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