How to call these variables?

in my client.lua I have the player coordinates being sent to the server using

local x, y, z = table.unpack(GetEntityCoords(ped, true))
TriggerServerEvent('sendpos', x, y, z) 

in the server.lua it’s recieved and stored using

RegisterServerEvent('sendpos')
AddEventHandler("sendpos", function(x, y, z) 
     local playercoords[source] = {
          ["x"] = x,
          ["y"] = y,
          ["z"] = z,
     }
end)

can someone please tell me a better way to store these variables and/or how to latter call these variables in the server.lua? I’ve tried using playercoords[source].x and playercoords[source]["x"] but neither of those seem to be working.

Well you are defining another local variable playercoords which means you can’t access it outside, also no need for ["x"] = x you can just x = x

You can also use vec3(x, y, z) and send it to the server/client so you don’t have to unpack and construct a new array with x,y,z

I don’t believe I can use vec3 because I need to call the coords individually from each other… So,

 local playercoords[source] = {
          x = x,
          y = y,
          z = z,
     }

can be called using

playercoords[source].x, playercoords[source].y, playercoords[source].z

?, sorry indra I’m a little rusty, been awhile since i’ve worked with lua, I’m trying to learn javascript, c#, and php at the same time so my mind is a little jumbled.

Remove the local, also you can get coordinates individually if it’s an vec3 like like .x and .y

playercoords[source] = vec3(x,y,z)? I’ve never actually used it so I don’t know.

@TheIndra

Why not just do that? It returns the vector3 as a table in Lua so you can do coords.x | coords.y | coords.z


-- CLIENT
local coords = GetEntityCoords(ped, false)
TriggerServerEvent("sendpos", coords)

-- SERVER
local playerdata = {}

RegisterServerEvent("sendpos")
AddEventHandler("sendpos", function(pos)
	playerdata[source]["position"] = pos
	print(json.encode(playerdata[source]["position"]))
end)
3 Likes