Jump to content

How to store getElementPosition in PlayerData Table?


Archinet

Recommended Posts

Hello guys of this excelent forum, im trying to get the GetElementPosition of my player inside a table PlayerData to store it after in a database, but no works, im not expert in Lua really, but i try to make it works, i search on the forum about this but i find nothing, i hope someone can help.

 

function savePlayer()
	if(isElement(source)) then
		if(getElementData(source, "Logueado") == false) then
			return true
		end
   	
   
	    local playerData = {

	    	x = getElementPosition(source).x, -- Here "Attempt to index a numbe value.
	    	y = getElementPosition(source).y,
	    	z = getElementPosition(source).z,
	    	rx = getElementRotation(source).rx,
	    	ry = getElementRotation(source).ry,
	    	rz = getElementRotation(source).rz,
	    	dinero = getPlayerMoney(source),
	    	trabajo = getElementData(source, "trabajo"),
	    	repartidor_nivel = getElementData(source, "repartidor_nivel"),
	    	repartidor_puntos = getElementData(source, "repartidor_puntos")
    	}

   		for k,v in ipairs(playerData) do

   			print(k.." - "..v)

   		end
	end
end
addEventHandler("onPlayerQuit", getRootElement(), savePlayer)

 

Edited by Archinet
Link to comment
  • Moderators
local x, y, z =  getElementPosition(source)

local xr, yr, zr = getElementRotation(source)

local playerData = {x=x, y=y, z=z, xr=xr, yr=yr, zr=zr}

Those 2 functions do not return a table, but multiple variables.

4 hours ago, Archinet said:

GetElementPosition of my player inside a table PlayerData

 

  • Thanks 1
Link to comment

 

8 hours ago, IIYAMA said:

local x, y, z =  getElementPosition(source)

local xr, yr, zr = getElementRotation(source)

local playerData = {x=x, y=y, z=z, xr=xr, yr=yr, zr=zr}

Those 2 functions do not return a table, but multiple variables.

 

Thanks so much @IIYAMA is good way! what you think about this method? is bad practice? i still learning xD

 

function pack(...)
	return {...}
end

pos = pack(getElementPosition(source))
  
  
playerData = {
    
	x = pos[1],
    y = pos[2],
    z = pos[3]
}
  

 

 

 

Edited by Archinet
  • Like 1
Link to comment
  • Moderators
1 minute ago, Archinet said:

 

Thanks so much @IIYAMA is good way, i was testing this method? is bad practice? i still learning xD

 


function pack(...)
	return {...}
end

pos = pack(getElementPosition(source))
  
  
playerData = {
    
	x = pos[1],
    y = pos[2],
    z = pos[3],
}
  

 

 

 

Creating a new table is a slower method, but nothing to worry about. You wouldn't notice the difference with the computers these days.

 

You didn't need the pack function to do this:

local pos = {getElementPosition(source)}

But if it helps you with readability, then why not a pack function?

 

 

 

  • Thanks 1
Link to comment
1 minute ago, IIYAMA said:

Creating a new table is a slower method, but nothing to worry about. You wouldn't notice the difference with the computers these days.

 

You didn't need the pack function to do this:


local pos = {getElementPosition(source)}

But if it helps you with readability, then why not a pack function?

 

 

 

I din't know that i can store with this method

local pos = {getElementPosition(source)}

Thanks for help me ? 

  • Like 1
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...