Jump to content

triggerServerEvent in onClientPlayerQuit doesnt work


Recommended Posts

function onPlayerWasted()
	triggerServerEvent("someEvent", localPlayer, data / 1000)
end
addEventHandler("onClientPlayerWasted", root, onPlayerWasted)

function onPlayerQuit()
    setTimer(function()
        triggerServerEvent("someEvent", localPlayer, data / 1000)
    end, 2000, 1) 
end
addEventHandler("onClientPlayerQuit", root, onPlayerQuit)


addEvent("someEvent", true)
addEventHandler("someEvent", root, function(data)
    if not exports.login:isPlayerLoggedIn(source) then return end 
	
    exports.login:updatePlayerStat(source, "stat", "stat", data)
end)

I would like to ask how can I make it so that when a player disconnects from the server, the data collected on the client side is sent to the server? It works fine with onClientPlayerWasted, but doesn't work with the onClientPlayerQuit, why?

Edited by Hiding
Link to comment
  • Moderators
7 hours ago, Hiding said:

but doesn't work with the onClientPlayerQuit, why?

Multiple reasons

  • onClientPlayerQuit is for remote players only. (Other players than yourself)
    • Currently when another player leaves, the stats of all other players are being saved, except yours.
  • When you quit the game, the client code on your system will stop.
    • Not sure if there is enough time to send data on onClientResourceStop + resourceRoot. But I wouldn't rely on it.
    • Using a timer to delay sending data, makes things really impossible.

 

Other issues:

  • onClientPlayerWasted should be attached to the localPlayer, not the root element. Else it also triggers for remote players.

  • addEventHandler("onClientPlayerWasted", localPlayer, onPlayerWasted)

     

  • The source from remote events can't be trusted, use the predefined variable client instead:
  • if not exports.login:isPlayerLoggedIn(client) then return end 
    	
    exports.login:updatePlayerStat(client, "stat", "stat", data)

 

 

 

  • Like 1
Link to comment

Thank you for your answer, then what would be your suggestion? Because I use an onClientRender to measure the distance driven by the player in km, and I would like to save it to db when the player leaves the server to avoid unnecessary dbExec

Link to comment
  • Moderators
4 minutes ago, Hiding said:

and I would like to save it to db when the player leaves the server to avoid unnecessary dbExec

For example trigger every 10 seconds a latent event. Those last <10 seconds do not really matter. You want to balance performance and accuracy.

To reduce server load and less db crash exploit able: Save it inside of the memory and write every 10/X minutes updates to the database. + onResourceStop

 

  • Like 1
Link to comment

Actually now I'm measuring the player's km on the server side, and I saving it when the player onPlayerQuit, but im using
setTimer(function()
     ...
end, 1000, 0) to measure it, can it be problem on the server, if it needs to save for example 10 players at the same time?

Link to comment
  • Moderators
27 minutes ago, Hiding said:

if it needs to save for example 10 players at the same time?

It depends on the amount of players and the server.

10 players should be fine.

More players? It is fine to save the the current km's inside of the memory. But writing it immediately to the database will definitely be a waste of performance, that can be spend else were.

  • Like 1
Link to comment
1 hour ago, IIYAMA said:

It depends on the amount of players and the server.

10 players should be fine.

More players? It is fine to save the the current km's inside of the memory. But writing it immediately to the database will definitely be a waste of performance, that can be spend else were.

Actually the km data is saved in the memory
 

local function initializePlayerData(player, km_driven)
    playerData[player] = {
        lastPosition = Vector3(getElementPosition(player)),
        distanceTraveled = km_driven * 1000, 
        newDistance = 0, 
    }
end


Only the final data that has been driven between login and quit will be saved in the db.

  • Like 1
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...