[Help]Onplayerdrop?

Is there a way to get the position where the player disconnected? does onplayerdrop really work? or there is any like onplayerdisconnect on clientside? i already have the loc save script but i don’t have any idea to save when player is quit

Just had a look at the server’s code and it looks like the playerDropped event is triggered before the client is removed from the server. So, you can probably get the client to send an event to the server before they disconnect with their location.

For example

AddEventHandler("playerDropped", function()
    TriggerClientEvent("getDroppedPos", source)
end)

RegisterServerEvent("playerDroppedPos")
AddEventHandler("playerDroppedPos", function( pos )
   print("Player " .. source .. " dropped out at " .. pos.x .. "," .. pos.y .. "," .. pos.z)
end)
RegisterNetEvent("getDroppedPos")
AddEventHandler("getDroppedPos", function()
   local pos = GetEntityCoords(GetPlayer(-1), true)
   -- Do something with the position
end)

Edit: Nevermind, I’m an idiot :slight_smile:

no, just send a position every 30 seconds or so

._.

Yeah, you can DEFINITELY send an event to a client that might just have been hit by a nuclear warhead, or, more likely, crashed.

At playerDropped, the client doesn’t exist anymore, there, finito, nothing that can be done about it, there’s some leftover data so you can do something to a name but nothing else.

anyway to do it on playerdisconnect?

playerDropped is player disconnect.

AddEventHandler('playerDropped', function(source, reason)
	print('Player disconnected: ' .. source)
end)
1 Like

How Can i get them position when they quit?

Do as helium said… Just send the position to the server, store it in an array or something then, when the player is dropped, you can just get the last-known position of the player.

Simply send an event from the client to the server every 30 seconds or so with their position, and update it on the server.

Server code:

serverPositions	= {}

function unloadPosForPlayer(source)
	serverPositions[source] = {0.0,0.0,0.0}
end

RegisterServerEvent('updatePosOnServerForPlayer')
AddEventHandler('updatePosOnServerForPlayer', function(newpos)
	serverPositions[source] = newpos
end)

function getPlayerPos(source)
    return table.unpack(serverPositions[source])
end

AddEventHandler('playerDropped', function(player)
    unloadPosForPlayer(source)
    print('Position cleared for player ' .. player .. ' with source ' .. source)
end)

Client:

--Every 5000 ms send the player coords to the server
Citizen.CreateThread(function()
    while true do
        local playerPed = GetPlayerPed(-1) --Gets the local player
        if playerPed and playerPed ~= -1 then -- Checks if the player exists
        	TriggerServerEvent('updatePosOnServerForPlayer', { table.unpack(GetEntityCoords(playerPed)) })
        end
        Citizen.Wait(5000)
    end
end)

the performance of the server will go worse? when too many player send the data to server every 5 sec?

Make the time longer. Or find a way to stagger it for every player