[HELP] How to place coordinates in Value using lua scripting?

Need help setting a variable to a set of coordinates!

Using lua, how is it possible to set a variable to be equal to static coordinates?
Normally, I see people use x, y, z = (getplayercoordinates), but in my cases, I’m going to be using a set of static coordinates and spawning things there, but to do so, I’ll need to know how to place the xyz values within a variable!

Thanks,
Wetter42

You have a couple of different options:

  1. You can do what you have already said, which is set a bunch of individual variables, however, that’s not very efficient.
local x, y, z = GetEntityCoords(PlayerPedId)
  1. You can store the coordinates in a table, this has the advantage of being easily loopable, should you need to; plus since it’s OOP, it’s neater and easier to access.
Coords = {}
table.insert(Coords, {Name = "Coord 1", x = 0, y = 0, z = 0})
table.insert(Coords, {Name = "Coord 2", x = 0, y = 0, z = 0})
table.insert(Coords, {Name = "Coord 3", x = 0, y = 0, z = 0})
  1. What I personally recommend, is that you store coordinates in Vector3 form, which is what natives like GetEntityCoords() return. If you are storing multiple coordinates in Vector3 form, you can use a mixture of Vector3 and tables to achieve OOP, which is very useful. Here is a working example from one of my resources.

    This is how I store the coordinates:

https://github.com/inferno-collection/Fire-EMS-Pager/blob/9b43699f5de49ff720ed4f40aac0479744761bef/[inferno-collection]/inferno-fire-ems-pager/client.lua#L38-L48

This is how I access them later on:

https://github.com/inferno-collection/Fire-EMS-Pager/blob/9b43699f5de49ff720ed4f40aac0479744761bef/[inferno-collection]/inferno-fire-ems-pager/client.lua#L372-L379

https://github.com/inferno-collection/Fire-EMS-Pager/blob/9b43699f5de49ff720ed4f40aac0479744761bef/[inferno-collection]/inferno-fire-ems-pager/client.lua#L865-L871

The variable names change, but it’s all the same data

Note, however, that Vector3 cannot be passed to the server with TriggerServerEvent, hence why I unpack the coordinates in that first blob


If you need any more help, just ask.

1 Like

This is great, and unlisted information chris! I had no clue that x, y, z = get entity coords were 3 separate variables - I just assumed they were one variable, but know I know it’s comma delimited!

Thanks so much, but I’m still a bit confused about 3 things:

  1. How are you calling those values within the blended table / vector3 format?

  2. How would you call / reference the specific array within the table if you chose to use option number two? (New to LUA / object-oriented programming by the way, so sorry if newb question) Is it the same way you’d do in the second option scenario?

  3. If you wanted to store a heading value as well, how would you do so?

Sorry if I’m being annoying!
Thanks,

So when the Vector3 value is within the table, you call it just like you would any other value:

MyTable = {}
MyTable.Example1 = 123456789
MyTable.Example2 = Vector3(123, 456, 789)

print(MyTable.Example1) -- Returns 123456789 (Int)
print(MyTable.Example2) -- Returns Vector3(123, 456, 789) (Vector3)
print(MyTable.Example2.x) -- Returns 123 (Int)
print(MyTable.Example2.y) -- Returns 456 (Int)
print(MyTable.Example2.z) -- Returns 789 (Int)

You can access the z, y, z of Vector3 just by calling like I did above; think of it like a table within a table, if you will.


So the way you access it really depends on the way you store it in the first place, and there a few different ways of doing that. Below is the same data, written a few different ways:

MyTable = {}
table.insert(MyTable, {Name = "Coord 1", x = 0, y = 0, z = 0})
MyTable = {}
MyTable["Coord 1"] = {Name = "Coord 1", x = 0, y = 0, z = 0}
MyTable = {}
MyTable.Coord1 = {Name = "Coord 1", x = 0, y = 0, z = 0}

When storing your data, you need to pick the method that will be easiest to call from in the future, and by that I mean, for example, if all you need to do with the data later on is indiscriminately loop through it without needing to worry about any kind of identifier, then my table.insert will work fine for you, because to access that data specifically is a pain in the arse:

-- Normal looping
for _, Data in ipairs(MyTable) do
    print(Data) -- Returns table (Table)
    print(Data["Name"]) -- Returns Coord 1 (String)
    print(Data["x"]) -- Returns 0 (Int)
    print(Data["y"]) -- Returns 0 (Int)
    print(Data["z"]) -- Returns 0 (Int)
end

-- Trying to find a specific entry
for _, Data in ipairs(MyTable) do
    if Data["Name"] == "Coord 1" then
        ...
    end
end

The reason you have to do it that way, is because the only identifying piece of information in the table, is the name value, however, it is just that, inside the table, so the only way to get to it is to access the table, either through a loop like above, or by accessing it’s specific table index, like so:

-- These are identical, and in this instance, interchangeable
print(MyTable[1]["Name"]) -- Returns Coord 1 (String)
print(MyTable[1].Name) -- Returns Coord 1 (String)

Like I said, this works just fine if you are, for example, just creating a bunch of blips on a map, and you could not care less about identifying information, however, if you do care about an identifier, you are best to store the data a bit differently. For example:

MyTable = {}
MyTable.MySpecificEntry = {x = 0, y = 0, z = 0}

print(MyTable.MySpecificEntry) -- Returns table (table)
print(MyTable.MySpecificEntry.x) -- Returns 0 (int)
print(MyTable.MySpecificEntry.y) -- Returns 0 (int)
print(MyTable.MySpecificEntry.z) -- Returns 0 (int)

The technical difference between this and the other example, is that while the Name value was unique, it was not the identifier of the entry, 1 would have been it’s identifier, since it was the first entry, whereas here, MySpecificEntry does not sit within the table itself, but rather it is the identifier of the table, so it can be accessed through MyTable.MySpecificEntry, whereas the other example cannot be.


If you want to do that, I would suggest using the blended table, and just putting the header in there as well, exactly as I have done in the working example I provided with Radius; just replace it with your heading value.


Sorry for any typos, not bothered to read through all this and fix them lol.

Not at all.

1 Like

Hey Chris! I’ve tried creating a script to have a jet spawn at the LSIA airport using the vehicle hash, and the vector3 coordinates stored within variable: coords, but can’t seem to get the damned thing spawned!

The code looks good to me, but maybe I’m missing something; What do you think?

function ShowNotification( text )
        SetNotificationTextEntry("STRING")
        AddTextComponentSubstringPlayerName(text)
        DrawNotification(false, false)
end

RegisterCommand('atest', function(source, args, rawCommand)
        local coords = Vector3(-970.75, -3355.18, 13.93)
        local veh = args[1]
        if veh == nil then veh = "jet" end
        vehiclehash = GetHashKey(veh)
        Requestmodel(vehiclehash)

        Citizen.CreateThread(function()
                local waiting = 0
                while not HasModelLoaded(vehiclehash) do
                        waiting = waiting + 100
                        Citizen.Wait(100)
                        if waiting > 5000 then
                                ShowNotification("~g~~b~Couldn't spawn the jet for whatever reason - Sorry!")
                                break
                        end
                end
                CreateVehicle(vehiclehash, coords, 33, true, false)
        end)
end)

Looks alright to me, I’m not at my PC at the moment, but I’ll test it shortly.

1 Like

Roger that - Thanks bud!

You know if you store the coords in a variable like

local myCoords = GetEntityCoords(PlayerPedId)

You can just access the x, y, z with

myCoords.x and myCoords.y and myCoords.z

Yep! Rather than getting the player’s ped, i’m trying to use STATIC coordinates - in this case, runway 33left in LSIA to be the testing grounds for this (P.S. We’ve discussed using getentitycoords() already in the previous threads!)

Only two issues:

  1. Lower case v for vector, I know I referred to it as Vector3, but I only capitalized it because it was grammatically correct to do so :stuck_out_tongue: Within code, it’s lowercase.

  2. RequestModel, not Requestmodel. https://runtime.fivem.net/doc/natives/#_0x963D27A58DF860AC

Other than that, works for me.

1 Like

Dude! awesome! I didn’t realize the fivem native resources were so anal with case sensitivity! This worked like a charm! :heart:

Now LAST, LAST, LAAAST question! Is there anything with regards to best practices that you wouldn’t do within this code, or something that was executed poorly (pun intended) within the code?

If not, thanks!

oh, damn! it seems I’ve forgotten one more thing - the heading; After trying to generate the heading within the CreateVehicle resource, it looks like it continually spawns at default heading of 0 or something of the like - any ideas?

At a glance, don’t think so; looks alright to me.


Can you show the updated code please.

Well, this is with the code that I’m currently using - If you try switching out the 33 for any value, it will still spawn in the same place - meaning the header value (for whatever reason) isn’t getting taken!

You don’t have to help if you’re done with this post - you’ve helped so much!

I’ll grab some breakfast then have a look. Get back to you in a bit.

Roger that! Thanks again!

Ok, so, not your fault this time. Even though the Native documentation says that the heading can be a number (int), it does actually need to be a float, which if you’re not familiar, means you just need to add .0 to the end of your number, to give it a decimal place.

Won’t work: 33

Will work: 33.0

Other than that, works for me :+1:

Sigh! Another One! Excellent; Thanks again for your help!

What I’ve learned from your help in this thread alone:

  • x, y, and z are independent variables
  • There are 3 different ways to store static variables (x, y, z / vector3, and by utilizing a table)
  • You can combine vector3 formatting and tables to make an easily loopable, compact, and efficient datastore
  • Vector3 can NOT be passed to the server with triggerserverevent
  • There are different ways to store the data and will effect how its called in the future
  • To store the heading, you can use a blended table
  • fivem resources can be a pain in the arse if you don’t know it’s case sensitive! (lol)
  • when working with certain resources that don’t work as ints, try floats first!

Thanks so much for your assistance over these two days! :heart: :clap:

1 Like