local hook = require 'samp.events'
local vector = require 'vector3d'

function main()
    repeat wait(0) until isSampAvailable()
    while not isSampAvailable() do wait(3500) end
    sampAddChatMessage("coordmaster loaded", -1)
    sampRegisterChatCommand('tpc', function()
        local _, x,y,z = getTargetBlipCoordinates()
        if not _ then
            printString("~r~checkpoint not found!",500)
        else
            if tp then
                tp = false
                sync = false
                setCharCoordinates(1,charPosX,charPosY,charPosZ)
            else
                if _ then
                    charPosX,charPosY,charPosZ = getCharCoordinates(1)
                    metkaX = x
                    metkaY = y
                    metkaZ = z
                    charPosZ = metkaZ
                    tp = true
                    sync = true
                end
            end
        end
  end)
  sampRegisterChatCommand('tpm', function()
    local _, x,y,z = SearchMarker()
    if not _ then
        printString("~r~marker not found!",500)
    else
        if tp then
            tp = false
            sync = false
            setCharCoordinates(1,charPosX,charPosY,charPosZ)
        else
            if _ then
                charPosX,charPosY,charPosZ = getCharCoordinates(1)
                metkaX = x
                metkaY = y
                metkaZ = z
                charPosZ = metkaZ
                tp = true
                sync = true
            end
        end
    end
end)
  while true do wait(0)
        if tp then
            if isCharInAnyCar(1) and getDriverOfCar(getCarCharIsUsing(1)) == 1 then
                if getDistanceBetweenCoords3d(metkaX,metkaY,metkaZ, charPosX, charPosY, charPosZ) >= 10 then
                    koordi = {}
                    sampForceVehicleSync()
                    local distan2 = getDistanceBetweenCoords2d(metkaX,metkaY, charPosX, charPosY)
                    local vectorX = metkaX - charPosX
                    local vectorY = metkaY - charPosY
                    local vectorZ = metkaZ - charPosZ
                    local vec = vector(vectorX, vectorY, vectorZ)
                    vec:normalize()
                    charPosX = charPosX + vec.x * 9
                                        charPosY = charPosY + vec.y * 9
                                        charPosZ = charPosZ + vec.z
                    sendOnfootSync(charPosX, charPosY, charPosZ)
                    setCharCoordinates(1,metkaX,metkaY,metkaZ)
                    printStringNow('~w~wait', 1000)
                    wait(35)
                else
                    sendOnfootSync(metkaX , metkaY , metkaZ)
                    setCarCoordinates(storeCarCharIsInNoSave(PLAYER_PED), metkaX , metkaY , metkaZ)
                    setCharCoordinates(1,charPosX,charPosY,charPosZ)
                    printStringNow('~w~teleported ~g~successfully', 1000)
                    tp = false
                end
            elseif not isCharInAnyCar(1) or getDriverOfCar(getCarCharIsUsing(1)) ~= 1 then
                tp = false
            end
        end
    end
end

function sendOnfootSync(x, y, z)
    local data = samp_create_sync_data('vehicle')
    data.position = {x = x, y = y, z = z}
    data.moveSpeed = {0.6, 0.6, -0.6}
        data.keysData = 0
    data.send()
end

function hook.onSendVehicleSync()
    if tp then
        return false
    end
end

function SearchMarker()
    local ret_posX = 0.0
    local ret_posY = 0.0
    local ret_posZ = 0.0
    local isFind = false
    for id = 0, 31 do
        local MarkerStruct = 0
        MarkerStruct = 0xC7F168 + id * 56
        local MarkerPosX = representIntAsFloat(readMemory(MarkerStruct + 0, 4, false))
        local MarkerPosY = representIntAsFloat(readMemory(MarkerStruct + 4, 4, false))
        local MarkerPosZ = representIntAsFloat(readMemory(MarkerStruct + 8, 4, false))
        if MarkerPosX ~= 0.0 or MarkerPosY ~= 0.0 or MarkerPosZ ~= 0.0 then
                ret_posX = MarkerPosX
                ret_posY = MarkerPosY
                ret_posZ = MarkerPosZ
                isFind = true
        end
    end
    return isFind, ret_posX, ret_posY, ret_posZ
end

function samp_create_sync_data(sync_type, copy_from_player)
        local ffi = require 'ffi'
        local sampfuncs = require 'sampfuncs'
        -- from SAMP.Lua
        local raknet = require 'samp.raknet'
        --require 'samp.synchronization'

        copy_from_player = copy_from_player or true
        local sync_traits = {
                player = {'PlayerSyncData', raknet.PACKET.PLAYER_SYNC, sampStorePlayerOnfootData},
                vehicle = {'VehicleSyncData', raknet.PACKET.VEHICLE_SYNC, sampStorePlayerIncarData},
                passenger = {'PassengerSyncData', raknet.PACKET.PASSENGER_SYNC, sampStorePlayerPassengerData},
                aim = {'AimSyncData', raknet.PACKET.AIM_SYNC, sampStorePlayerAimData},
                trailer = {'TrailerSyncData', raknet.PACKET.TRAILER_SYNC, sampStorePlayerTrailerData},
                unoccupied = {'UnoccupiedSyncData', raknet.PACKET.UNOCCUPIED_SYNC, nil},
                bullet = {'BulletSyncData', raknet.PACKET.BULLET_SYNC, nil},
                spectator = {'SpectatorSyncData', raknet.PACKET.SPECTATOR_SYNC, nil}
        }
        local sync_info = sync_traits[sync_type]
        local data_type = 'struct ' .. sync_info[1]
        local data = ffi.new(data_type, {})
        local raw_data_ptr = tonumber(ffi.cast('uintptr_t', ffi.new(data_type .. '*', data)))
        -- copy player's sync data to the allocated memory
        if copy_from_player then
                local copy_func = sync_info[3]
                if copy_func then
                        local _, player_id
                        if copy_from_player == true then
                                _, player_id = sampGetPlayerIdByCharHandle(PLAYER_PED)
                        else
                                player_id = tonumber(copy_from_player)
                        end
                        copy_func(player_id, raw_data_ptr)
                end
        end
        -- function to send packet
        local func_send = function()
                local bs = raknetNewBitStream()
                raknetBitStreamWriteInt8(bs, sync_info[2])
                raknetBitStreamWriteBuffer(bs, raw_data_ptr, ffi.sizeof(data))
                raknetSendBitStreamEx(bs, sampfuncs.HIGH_PRIORITY, sampfuncs.UNRELIABLE_SEQUENCED, 1)
                raknetDeleteBitStream(bs)
        end
        -- metatable to access sync data and 'send' function
        local mt = {
                __index = function(t, index)
                        return data[index]
                end,
                __newindex = function(t, index, value)
                        data[index] = value
                end
        }
        return setmetatable({send = func_send}, mt)
end
Оптимизируй код, улучши его, используй тернарные операторы, используй современные методы, улучши способы и циклы.