New to LUA Dev - Need basic help

So I’m new to LUA Development, trying to teach myself the basics before I get stuck in. I’m trying to set up a basic teleport script (currently with just one location set) and I want the script to know if the player is in a vehicle and whether to teleport the player alone or the vehicle with the player in it but it’s not working. I’m only teleporting the player.

Here is where I am at.

local v1 = vector2(101.94, -1937.1)
local v2 = vector3(101.94, -1937.1, 20.8)
local v3 = vector4(101.94, -1937.1, 20.8, 47.78)

local player = PlayerPedId()
local vehicle = GetVehiclePedIsUsing(player)
local pedInVehicle = false;

if vehicle == nil then
pedInVehicle = true;
pedInVehicle = false;

RegisterCommand(‘tp’, function()
– if player is in vehicle
if pedInVehicle == false then
– teleport vehicle
SetEntityCoords(player, v3, false, false, false, false)
– else player is not in vehicle
– teleport player
SetEntityCoords(vehicle, v3, false, false, false, false)

I have also tried

if IsPedInVehicle(player, vehicle, true) then
pedInVehicle = true
pedInVehicle = false

Hello, you might be interested in this native : SetPedCoordsKeepVehicle - Natives @ Docs

I left you some notes so you can hopefully see and understand why I did something, if you have any questions please let me know, I am happy to help :slight_smile: .

local locations = { -- Creating a table to make things more "orginized".
    v1 = vec2(101.94, -1937.1),
    v2 = vec3(101.94, -1937.1, 20.8),
    v3 = vec4(101.94, -1937.1, 20.8, 47.78) -- [Vector 4 is x, y, z, HEADING so you can't use then when something asks for an x, y, z unless you specify such as locations.v3.x, locations.v3.y, etc]

RegisterCommand('tp', function()
        Always put your variables inside of a command when possible, 
        so it checks when the command is used, so for example, 
        if you check if the player is in the vehicle before the command is used, 
        then they might not be in one at that time, but when they do get in a car, 
        it won't be updated, so instead we check when the command is used

    local ped = PlayerPedId() -- Player
    local inVeh = IsPedInAnyVehicle(ped, true) -- Checking if in any vehicle

    if inVeh then -- If in a vehicle, then we get the vehicle the ped is in.
        local vehicle = GetVehiclePedIsIn(ped, false) -- The vehicle the ped is in.
        SetEntityCoords(vehicle, locations.v3.x, locations.v3.y, locations.v3.z, false, false, false, false)
        SetEntityHeading(vehicle, locations.v3.w)
        SetEntityCoords(ped, locations.v3.x, locations.v3.y, locations.v3.z, false, false, false, false)
        SetEntityHeading(ped, locations.v3.w)

That makes perfect sense! lol. I’m so used to CPP where everything goes at the start, I didn’t think to put it in the command itself. Thank you so much! :smiley:

I kept thinking “If this was CPP, this would be fucking perfect!” lol

I do wonder, however, are you sure I can’t just use

SetEntityCoords(ped, v3, false, false, false, false)

I only ask because, when I was using it earlier like that, it was teleporting my player with the heading I wanted. I even adjusted the heading to get it pointed at the heading I wanted

Nevermind, I tested it out and I was wrong. lol

Here’s a tip: for SetEntityCoords you only need to pass the entity and the vector3.

 SetEntityCoords(vehicle, locations.v3.x, locations.v3.y, locations.v3.z, false, false, false, false)


SetEntityCoords(vehicle, locations.v3)

Well that’s what I thought, but it didn’t keep the heading I wanted

SetEntityCoords Only sets the coords, not the heading, thats why SetEntityHeading exists.

I’ve got a few more questions if you’re willing to help? I can’t DM you, so if you’re willing, would you be able to DM me so we can start a conversation?

You can also DM me for help if you want.