[Help]Onplayerdrop?


#1

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


#2

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:


#3

no, just send a position every 30 seconds or so


#4

._.

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.


#5

anyway to do it on playerdisconnect?


#6

playerDropped is player disconnect.

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

#7

How Can i get them position when they quit?


#8

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.


#9

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)

#10

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


#11

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