Murilo_apa Posted November 30, 2022 Posted November 30, 2022 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)
AngelAlpha Posted November 30, 2022 Posted November 30, 2022 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)
Murilo_apa Posted November 30, 2022 Author Posted November 30, 2022 hmm, position is stored in table...
AngelAlpha Posted November 30, 2022 Posted November 30, 2022 5 hours ago, Murilo_apa said: hmm, position is stored in table... setAccountData(getPlayerAccount(pl), "save:lastpos", toJSON({x, y, z}))
mafioz Posted November 30, 2022 Posted November 30, 2022 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)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now