I didn't read your code thoroughly, but loading at authorized, saving at disconnect, and closing at plugin_end() is the ideal way for storing data, IMO. It doesn't matter where you place the function in your code.
I actually did this but when the server get shut down the data doesn't get saved, I added code for saving the data every 30 second for that reason.
/* Save the data using ( SteamID, Name, IP ) * SteamID = 1 - Name = 2 - IP = 3 */ g_iCvars[SAVE_DATA] = register_cvar( "save_type", "1"); g_iCvars[LIMIT] = register_cvar( "limit_cash", "1"); //Change to 0 if u want to be able to withdraw / deposit more than 16k$ g_iCvars[MAX_CASH] = register_cvar( "max_cash", "16000"); // Maximum cash withdraw / deposit from/to the bank g_iCvars[MAX_CASH_SAVE] = register_cvar( "max_cash", "100000000"); // Maximum cash saving in the bank
public plugin_cfg() { set_task(AUTO_SAVE, "AutoSave", _, _, _, "b") }
public AutoSave() { for( new i ; i < g_iMaxPlayers ; i++ ) { if( is_user_connected(i) ) ReadData(i, SAVE); } }
public client_authorized(id) { get_user_authid( id , g_iPlayerData[ id ][ AuthID ] , charsmax( g_iPlayerData[ ][ AuthID ] ) ); get_user_name( id , g_iPlayerData[ id ][ Name ] , charsmax( g_iPlayerData[ ][ Name ] ) ); get_user_ip( id, g_iPlayerData[ id ][ IP ] , charsmax(g_iPlayerData[ ][ IP ]), 1 );
new const iData[][] = { "Deposit", "Deposit All^n", "Withdraw", "Withdraw All^n", "Check Your Cash", "Donate", "Reset Bank" }
for( new i ; i < sizeof ( iData ) ; i++ ) { formatex(szData, charsmax(szData), "%s", iData[i]); menu_additem(iMenu, szData, ""); }
menu_setprop(iMenu, MPROP_EXITNAME, "\rClose")
menu_display(id, iMenu) return PLUGIN_HANDLED }
public Handler(id, iMenu, iItem) { if(iItem != MENU_EXIT) { switch(iItem) { case 0: { client_cmd(id, "messagemode DepositCash"); set_hudmessage(255, 255, 85, 0.01, 0.18, 2, 0.5, 6.0, 0.05, 0.05, -1); show_hudmessage(id, "Type how much you want to deposit."); } case 1: DepositAllCash(id); case 2: { client_cmd(id, "messagemode TakeCash"); set_hudmessage(255, 255, 85, 0.01, 0.18, 2, 0.5, 6.0, 0.05, 0.05, -1); show_hudmessage(id, "Type how much you want to take."); } case 3: WithdrawAllCash(id); case 4: CC_SendMessage(id,"Your Cash is:^4 %d", g_iPlayerData[ id ][ Cash ]); case 5: DonateMenu(id) case 6: ResetMenu(id) } } menu_destroy(iMenu); return PLUGIN_HANDLED }
/* 1. UserID 2. Menu Access 3. Menu Title 4. Menu Handler 5. Player Flags - P.s: 's' = do not include self client */ public DonateMenu(id) { PlayerMenu ( id, " ", "\yChoose a player: \r[Donate]", "Donate_Handler", "chs" ) }
public ResetMenu(id) { PlayerMenu ( id, "a", "\yChoose a player: \r[Reset Bank]", "Reset_Handler", "chs" ) }
public DepositAllCash(id) { new iCash = get_user_cash(id);
if( iCash <= 0) { CC_SendMessage(id,"You do not have enough money or invalid value."); return PLUGIN_CONTINUE; }
if( g_iPlayerData[ id ][ Cash ] > get_pcvar_num(g_iCvars[MAX_CASH_SAVE]) ) { CC_SendMessage(id,"Your bank is full."); return PLUGIN_CONTINUE }
CC_SendMessage(id,"You have deposit^4 %i.", iCash); g_iPlayerData[ id ][ Cash ] += iCash; set_user_cash(id, iCash - iCash); ReadData(id, SAVE);
return PLUGIN_HANDLED; }
public WithdrawAllCash(id) { new iCash = get_user_cash(id); new iBankCash;
if( g_iPlayerData[ id ][ Cash ] <= 0) { CC_SendMessage(id,"You do not have enough money or invalid value."); return PLUGIN_CONTINUE }
if( get_pcvar_num(g_iCvars[LIMIT]) != 1) { CC_SendMessage(id,"You have withdraw^4 %i.", g_iPlayerData[ id ][ Cash ]); g_iPlayerData[ id ][ Cash ] -= g_iPlayerData[ id ][ Cash ] set_user_cash(id, iCash + g_iPlayerData[ id ][ Cash ]); } else { iBankCash = get_pcvar_num(g_iCvars[MAX_CASH]) - iCash;
if(g_iPlayerData[ id ][ Cash ] < iBankCash) { CC_SendMessage(id,"You have withdraw^4 %i.", g_iPlayerData[ id ][ Cash ]); set_user_cash(id, iCash + g_iPlayerData[ id ][ Cash ]); g_iPlayerData[ id ][ Cash ] -= g_iPlayerData[ id ][ Cash ]; } else { CC_SendMessage(id,"You have withdraw^4 %i.", g_iPlayerData[ id ][ Cash ]); set_user_cash(id, iCash + iBankCash); g_iPlayerData[ id ][ Cash ] -= iBankCash; } }
if( g_iPlayerData[ id ][ Cash ] < iValue || iValue <= 0) { CC_SendMessage(id,"You do not have enough money or invalid value."); return PLUGIN_CONTINUE }