Simple Player Coord Saving

I’m looking for a .txt player save system as simple as it gets, I’m finding a lot about mysql and that’s great, but I would love to see more support on being standalone, since it’s only the player’s last coord.

This sounds like a great use for the Key Value Pair natives (search KVP on Native Reference - Cfx.re Docs) you would just have to convert the vector3 to a json, and it would probably be optimal to use the non-sync version (see FlushResourceKvp - Natives @ Cfx.re Docs)

Here is an example, make sure the data folder is exists in your resource folder.

function GetPlayerLicense(playerId)
    for k, v in ipairs(GetPlayerIdentifiers(playerId)) do
		if string.match(v, 'license:') then
			return string.gsub(v, 'license:', '')
		end
	end
end

function GetPlayerCoords(playerId)
    local playerLicense = GetPlayerLicense(playerId)
    local file = LoadResourceFile(GetCurrentResourceName(), 'data/' .. playerLicense .. '.txt')
    return file and json.decode(file) or { x = 0, y = 0, z = 0 }
end

function SavePlayerCoords(playerId)
    local playerPed = GetPlayerPed(playerId)
    local playerLicense = GetPlayerLicense(playerId)
    if DoesEntityExist(playerPed) then
        SaveResourceFile(GetCurrentResourceName(), 'data/' .. playerLicense .. '.txt', json.encode(GetEntityCoords(playerPed)))
    end
end

-- Update players coords each 30 seconds
function UpdatePlayerCoords()
    for _, playerId in ipairs(GetPlayers()) do
        SavePlayerCoords(playerId)
    end
    SetTimeout(30000, UpdatePlayerCoords)
end

UpdatePlayerCoords()

-- Update players coords when disconnecting
AddEventHandler('playerDropped', function ()
    SavePlayerCoords(source)
end)

This reply is excellent, thank you for your example as it helps me alot. Apon testing it seems to properly ID the player and create the . TXT with success. But the files saves the cord with 0,0,0 I’m not sure if I’m missing something very obvious as usual :wink:

I forgot to tell you, if you want to use GetPlayerPed and GetEntityCoords in server side, you need to enable OneSync. Otherwise, you can get player’s coords from client side and send it to server side.

There must be a way to have this grab players coords on logout, but prevent consistent coord saving overriding while the player is currently online already so they could type /oldpos to attain there previous location, and yet only let the chat command be accessed the one time. Is there any possibility you could briefly assist me?