Jump to content

How I can persist position data? Persist position data! [HELP]


Murilo_apa

Recommended Posts

How I can persist position data? This is my current code:

function saveAccountData()
    local position = getElementPosition(source)

    setAccountData(account, 'accountData.position', position)

end

addEventHandler('onResourceStop', root, function()
    for i,player in ipairs(getElementsByType('player')) do
        local position = getElementPosition(player)
        if not isGuestAccount(account) then
            setAccountData(account, 'accountData.position', position)
        end
    end
end)

function loadAccountData(_, account
    local positiondata = getAccountData(account, 'accountData.position')
    local position = tonumber(positiondata)
    if position then
        setElementPosition(source, position)
    end
end

addEventHandler('onPlayerQuit', root, saveAccountData)
addEventHandler('onPlayerLogin', root, loadAccountData)
Link to comment
function save (pl)
	local x, y, z = getElementPosition(pl)
	setAccountData(getPlayerAccount(pl), "save:lastpos", toJSON(x, y, z))
end

function load (pl)
	local pos = getAccountData(getPlayerAccount(pl), "save:lastpos")
	if not pos then return end
	
	setElementPosition (pl, fromJSON(pos))
end

addEventHandler ("onPlayerLogin", root, function() load (source) end)

addEventHandler ("onPlayerQuit", root, function () save (source) end)
addEventHandler ("onResourceStop", resourceRoot, function()
	for i, v in ipairs (getElementsByType("player")) do
		save(v)
	end
end)

 

Link to comment
function save (player)
	local x, y, z = getElementPosition(player)
	setAccountData(getPlayerAccount(player), "save:lastpos", toJSON({x, y, z}))
end

function load (player)
	local pos = getAccountData(getPlayerAccount(player), "save:lastpos")
	if not pos then return end
	
    local x, y, z = unpack(fromJSON(pos))
	setElementPosition(player, x, y, z)
end

addEventHandler ("onPlayerLogin", root, function() load (source) end)

addEventHandler ("onPlayerQuit", root, function () save (source) end)
addEventHandler ("onResourceStop", resourceRoot, function()
	for _, player in ipairs (getElementsByType("player")) do
		save(player)
	end
end)

 

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...