LimoN>w< Posted April 5, 2018 Share Posted April 5, 2018 Buenas, yo tengo el resource de tiempo en el servidor, el cual guarda el tiempo total que el jugador tiene en el servidor (se haya desconectado o no) el problema es cuando se trata de darle stop o restart al resource, ya que el mismo una vez parado el resource o reiniciado borra todos los datos y se pierden Obviamente el stop lo he hecho porque he tenido problemas con el scoreboard ya que se ha bugeado y el tiempo en el servidor no aparecía y tenia que reiniciar el resource ocasionando que las horas de los jugadores se perdieran Si alguien me puede ayudar le agredeceria muchísimo! Dejo el codigo lua debajo exports.scoreboard:addScoreboardColumn('Online') local t = { } function checkValues( source,arg1,arg2) if (arg2 >= 60) then t[ source ][ 'min' ] = tonumber( t[ source ][ 'min' ] or 0 ) + 1 t[ source ][ 'sec' ] = 0 end if (arg1 >= 60) then t[ source ][ 'min' ] = 0 t[ source ][ 'hour' ] = tonumber( t[ source ][ 'hour' ] or 0 ) + 1 end return arg1, arg2 end setTimer( function( ) for _, v in pairs( getElementsByType( "player" ) ) do if (not t[ v ]) then t[ v ] = { ["hour"] = 0, ["min"] = 0, ["sec"] = 0 } end t[ v ][ 'sec' ] = tonumber( t[ v ][ 'sec' ] or 0 ) + 1 local min,sec = checkValues ( v, t[ v ][ 'min' ] or 0, t[ v ][ 'sec' ] or 0 ) local hour = tonumber( t[ v ][ 'hour' ] or 0 ) setElementData( v, "Online", tostring( hour )..':'..tostring( min )..':'..tostring( sec ) ) end end, 1000, 0 ) function onPlayerQuit ( ) local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) and not isGuestAccount ( playeraccount ) then local sValue = getElementData( source,'Online' ) local hour = tonumber( t[ source ][ 'hour' ] or 0 ) local min = tonumber( t[ source ][ 'min' ] or 0 ) local sec = tonumber( t[ source ][ 'sec' ] or 0 ) setAccountData ( playeraccount, "Online-hour", tostring(hour) ) setAccountData ( playeraccount, "Online-min", tostring(min) ) setAccountData ( playeraccount, "Online-sec", tostring(sec) ) setAccountData ( playeraccount, "Online", tostring(sValue) ) end t[ source ] = nil end function onPlayerLogin (_, playeraccount ) if ( playeraccount ) then local time = getAccountData ( playeraccount, "Online" ) local hou = getAccountData ( playeraccount, "Online-hour") local min = getAccountData ( playeraccount, "Online-min") local sec = getAccountData ( playeraccount, "Online-sec") if ( time ) then setElementData ( source, "Online", time ) t[ source ]["hour"] = tonumber(hou) t[ source ]["min"] = tonumber(min) t[ source ]["sec"] = tonumber(sec) else setElementData ( source, "Online",0 ) setAccountData ( playeraccount, "Online",0 ) end end end addEventHandler ( "onPlayerQuit", root, onPlayerQuit ) addEventHandler ( "onPlayerLogin", root, onPlayerLogin ) Link to comment
Simple0x47 Posted April 5, 2018 Share Posted April 5, 2018 Lo que pasa es que el evento de 'OnResourceStop' no esta controlado. Algo por el estilo debería valerte. local function turn_off() local players = getElementsByType( 'player' ) for player_id = 1, #players do local player = players[ player_id ] local player_account = getPlayerAccount( player ) if ( ( player_account ) and not ( isGuestAccount( player_account ) ) ) then local time_data = getElementData( player, 'Online' ) local online_keys = { { 'hour', 'Online-hour' }, { 'min', 'Online-min' }, { 'sec', 'Online-sec' }, 'Online' } for i = 1, #online_keys do if ( i == #online_keys ) then setAccountData( player_account, online_keys[ i ], time_data ) else setAccountData( player_account, online_keys[ i ][ 2 ], time_data[ i ][ 1 ] or 0 ) end end end end end addEventHandler( "onResourceStop", resourceRoot, turn_off ) Link to comment
LimoN>w< Posted April 6, 2018 Author Share Posted April 6, 2018 14 minutes ago, Simple01 said: Lo que pasa es que el evento de 'OnResourceStop' no esta controlado. Algo por el estilo debería valerte. local function turn_off() local players = getElementsByType( 'player' ) for player_id = 1, #players do local player = players[ player_id ] local player_account = getPlayerAccount( player ) if ( ( player_account ) and not ( isGuestAccount( player_account ) ) ) then local time_data = getElementData( player, 'Online' ) local online_keys = { { 'hour', 'Online-hour' }, { 'min', 'Online-min' }, { 'sec', 'Online-sec' }, 'Online' } for i = 1, #online_keys do if ( i == #online_keys ) then setAccountData( player_account, online_keys[ i ], time_data ) else setAccountData( player_account, online_keys[ i ][ 2 ], time_data[ i ][ 1 ] or 0 ) end end end end end addEventHandler( "onResourceStop", resourceRoot, turn_off ) Eso lo agrego despues de la ultima linea del lua del resource original? Link to comment
Simple0x47 Posted April 6, 2018 Share Posted April 6, 2018 Donde quieras, con que este en el resource situado en el server-side, debería ir bien. Link to comment
Sticmy Posted April 7, 2018 Share Posted April 7, 2018 Yo lo haría así es más fácil exports.scoreboard:addScoreboardColumn('Online') local t = { } function checkValues( source,arg1,arg2) if (arg2 >= 60) then t[ source ][ 'min' ] = tonumber( t[ source ][ 'min' ] or 0 ) + 1 t[ source ][ 'sec' ] = 0 end if (arg1 >= 60) then t[ source ][ 'min' ] = 0 t[ source ][ 'hour' ] = tonumber( t[ source ][ 'hour' ] or 0 ) + 1 end return arg1, arg2 end setTimer( function( ) for _, v in pairs( getElementsByType( "player" ) ) do if (not t[ v ]) then t[ v ] = { ["hour"] = 0, ["min"] = 0, ["sec"] = 0 } end t[ v ][ 'sec' ] = tonumber( t[ v ][ 'sec' ] or 0 ) + 1 local min,sec = checkValues ( v, t[ v ][ 'min' ] or 0, t[ v ][ 'sec' ] or 0 ) local hour = tonumber( t[ v ][ 'hour' ] or 0 ) setElementData( v, "Online", tostring( hour )..':'..tostring( min )..':'..tostring( sec ) ) end end, 1000, 0 ) function onPlayerQuit ( ) local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) and not isGuestAccount ( playeraccount ) then local sValue = getElementData( source,'Online' ) local hour = tonumber( t[ source ][ 'hour' ] or 0 ) local min = tonumber( t[ source ][ 'min' ] or 0 ) local sec = tonumber( t[ source ][ 'sec' ] or 0 ) setAccountData ( playeraccount, "Online-hour", tostring(hour) ) setAccountData ( playeraccount, "Online-min", tostring(min) ) setAccountData ( playeraccount, "Online-sec", tostring(sec) ) setAccountData ( playeraccount, "Online", tostring(sValue) ) end t[ source ] = nil end addEvent ("stopSave", true) addEventHandler ("stopSave", getRootElement(), onQuit) function onPlayerLogin (_, playeraccount ) if ( playeraccount ) then local time = getAccountData ( playeraccount, "Online" ) local hou = getAccountData ( playeraccount, "Online-hour") local min = getAccountData ( playeraccount, "Online-min") local sec = getAccountData ( playeraccount, "Online-sec") if ( time ) then setElementData ( source, "Online", time ) t[ source ]["hour"] = tonumber(hou) t[ source ]["min"] = tonumber(min) t[ source ]["sec"] = tonumber(sec) else setElementData ( source, "Online",0 ) setAccountData ( playeraccount, "Online",0 ) end end end addEventHandler ( "onPlayerQuit", root, onPlayerQuit ) addEventHandler ( "onPlayerLogin", root, onPlayerLogin ) function onStop () for k, v in ipairs (getElementsByType("player")) do if not (isGuestAccount (getPlayerAccount (v))) then triggerEvent ("stopSave", v) end end end addEventHandler ("onResourceStop", resourceRoot, onStop) Link to comment
Simple0x47 Posted April 7, 2018 Share Posted April 7, 2018 2 hours ago, DostMaster said: Yo lo haría así es más fácil exports.scoreboard:addScoreboardColumn('Online') local t = { } function checkValues( source,arg1,arg2) if (arg2 >= 60) then t[ source ][ 'min' ] = tonumber( t[ source ][ 'min' ] or 0 ) + 1 t[ source ][ 'sec' ] = 0 end if (arg1 >= 60) then t[ source ][ 'min' ] = 0 t[ source ][ 'hour' ] = tonumber( t[ source ][ 'hour' ] or 0 ) + 1 end return arg1, arg2 end setTimer( function( ) for _, v in pairs( getElementsByType( "player" ) ) do if (not t[ v ]) then t[ v ] = { ["hour"] = 0, ["min"] = 0, ["sec"] = 0 } end t[ v ][ 'sec' ] = tonumber( t[ v ][ 'sec' ] or 0 ) + 1 local min,sec = checkValues ( v, t[ v ][ 'min' ] or 0, t[ v ][ 'sec' ] or 0 ) local hour = tonumber( t[ v ][ 'hour' ] or 0 ) setElementData( v, "Online", tostring( hour )..':'..tostring( min )..':'..tostring( sec ) ) end end, 1000, 0 ) function onPlayerQuit ( ) local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) and not isGuestAccount ( playeraccount ) then local sValue = getElementData( source,'Online' ) local hour = tonumber( t[ source ][ 'hour' ] or 0 ) local min = tonumber( t[ source ][ 'min' ] or 0 ) local sec = tonumber( t[ source ][ 'sec' ] or 0 ) setAccountData ( playeraccount, "Online-hour", tostring(hour) ) setAccountData ( playeraccount, "Online-min", tostring(min) ) setAccountData ( playeraccount, "Online-sec", tostring(sec) ) setAccountData ( playeraccount, "Online", tostring(sValue) ) end t[ source ] = nil end addEvent ("stopSave", true) addEventHandler ("stopSave", getRootElement(), onQuit) function onPlayerLogin (_, playeraccount ) if ( playeraccount ) then local time = getAccountData ( playeraccount, "Online" ) local hou = getAccountData ( playeraccount, "Online-hour") local min = getAccountData ( playeraccount, "Online-min") local sec = getAccountData ( playeraccount, "Online-sec") if ( time ) then setElementData ( source, "Online", time ) t[ source ]["hour"] = tonumber(hou) t[ source ]["min"] = tonumber(min) t[ source ]["sec"] = tonumber(sec) else setElementData ( source, "Online",0 ) setAccountData ( playeraccount, "Online",0 ) end end end addEventHandler ( "onPlayerQuit", root, onPlayerQuit ) addEventHandler ( "onPlayerLogin", root, onPlayerLogin ) function onStop () for k, v in ipairs (getElementsByType("player")) do if not (isGuestAccount (getPlayerAccount (v))) then triggerEvent ("stopSave", v) end end end addEventHandler ("onResourceStop", resourceRoot, onStop) Eso te digo que no funcionaría, y por una razón muy sencilla. Link to comment
Sticmy Posted April 7, 2018 Share Posted April 7, 2018 4 hours ago, Simple01 said: Eso te digo que no funcionaría, y por una razón muy sencilla. Pues la verdad que ami me ha funcionado siempre Link to comment
Simple0x47 Posted April 7, 2018 Share Posted April 7, 2018 Intenta el código, no es un error de planteamiento. Si no uno de argumentos. Link to comment
LimoN>w< Posted April 7, 2018 Author Share Posted April 7, 2018 20 hours ago, Simple01 said: Donde quieras, con que este en el resource situado en el server-side, debería ir bien. Lo puse debajo toda la linea que me pasaron y al stopear el resource los datos siguen sin guardarse Link to comment
Simple0x47 Posted April 8, 2018 Share Posted April 8, 2018 6 hours ago, LimoN>w< said: Lo puse debajo toda la linea que me pasaron y al stopear el resource los datos siguen sin guardarse Asegurate de que el valor que tienen en el elementData 'Online' es el deseado. Ya que se puede deber a que dicha key almacene un valor núlo. 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