Вопросы по Lua скриптингу

Общая тема для вопросов по разработке скриптов на языке программирования Lua, в частности под MoonLoader.
  • Задавая вопрос, убедитесь, что его нет в списке частых вопросов и что на него ещё не отвечали (воспользуйтесь поиском).
  • Поищите ответ в теме посвященной разработке Lua скриптов в MoonLoader
  • Отвечая, убедитесь, что ваш ответ корректен.
  • Старайтесь как можно точнее выразить мысль, а если проблема связана с кодом, то обязательно прикрепите его к сообщению, используя блок [code=lua]здесь мог бы быть ваш код[/code].
  • Если вопрос связан с MoonLoader-ом первым делом желательно поискать решение на wiki.

Частые вопросы

Как научиться писать скрипты? С чего начать?
Информация - Гайд - Всё о Lua скриптинге для MoonLoader(https://blast.hk/threads/22707/)
Как вывести текст на русском? Вместо русского текста у меня какие-то каракули.
Изменить кодировку файла скрипта на Windows-1251. В Atom: комбинация клавиш Ctrl+Shift+U, в Notepad++: меню Кодировки -> Кодировки -> Кириллица -> Windows-1251.
Как получить транспорт, в котором сидит игрок?
Lua:
local veh = storeCarCharIsInNoSave(PLAYER_PED)
Как получить свой id или id другого игрока?
Lua:
local _, id = sampGetPlayerIdByCharHandle(PLAYER_PED) -- получить свой ид
local _, id = sampGetPlayerIdByCharHandle(ped) -- получить ид другого игрока. ped - это хендл персонажа
Как проверить, что строка содержит какой-то текст?
Lua:
if string.find(str, 'текст', 1, true) then
-- строка str содержит "текст"
end
Как эмулировать нажатие игровой клавиши?
Lua:
local game_keys = require 'game.keys' -- где-нибудь в начале скрипта вне функции main

setGameKeyState(game_keys.player.FIREWEAPON, -1) -- будет сэмулировано нажатие клавиши атаки
Все иды клавиш находятся в файле moonloader/lib/game/keys.lua.
Подробнее о функции setGameKeyState здесь: lua - setgamekeystate | BlastHack — DEV_WIKI(https://www.blast.hk/wiki/lua:setgamekeystate)
Как получить id другого игрока, в которого целюсь я?
Lua:
local valid, ped = getCharPlayerIsTargeting(PLAYER_HANDLE) -- получить хендл персонажа, в которого целится игрок
if valid and doesCharExist(ped) then -- если цель есть и персонаж существует
  local result, id = sampGetPlayerIdByCharHandle(ped) -- получить samp-ид игрока по хендлу персонажа
  if result then -- проверить, прошло ли получение ида успешно
    -- здесь любые действия с полученным идом игрока
  end
end
Как зарегистрировать команду чата SAMP?
Lua:
-- До бесконечного цикла/задержки
sampRegisterChatCommand("mycommand", function (param)
     -- param будет содержать весь текст введенный после команды, чтобы разделить его на аргументы используйте string.match()
    sampAddChatMessage("MyCMD", -1)
end)
Крашит игру при вызове sampSendChat. Как это исправить?
Это происходит из-за бага в SAMPFUNCS, когда производится попытка отправки пакета определенными функциями изнутри события исходящих RPC и пакетов. Исправления для этого бага нет, но есть способ не провоцировать его. Вызов sampSendChat изнутри обработчика исходящих RPC/пакетов нужно обернуть в скриптовый поток с нулевой задержкой:
Lua:
function onSendRpc(id)
  -- крашит:
  -- sampSendChat('Send RPC: ' .. id)

  -- норм:
  lua_thread.create(function()
    wait(0)
    sampSendChat('Send RPC: ' .. id)
  end)
end
 
Последнее редактирование:

DON GANDON

Участник
18
0
[ML] (error) 123.lua: opcode '0984' call caused an unhandled exception
stack traceback:
[C]: in function 'getObjectModel'
D:\GTA 130K BY DAPO SHOW\moonloader\123.lua:18: in function <D:\GTA 130K BY DAPO SHOW\moonloader\123.lua:6>
[ML] (error) 123.lua: Script died due to an error. (0E60B53C)
Вот такая ошибка вылетает, когда кончаются модели с ид 1294, как пофиксить это?

код:
require 'moonloader'

local work = false
local result = false
local hook = require 'samp.events'

function main()
    if not isSampLoaded() or not isSampfuncsLoaded() then return end
    while not isSampAvailable() do wait(100) end
 
    sampRegisterChatCommand('tps', function()
        work = not work
        sampAddChatMessage(work and '{ffffff}Активирован' or '{ffffff}Деактивирован', -1)
    end)
    while true do
        wait(0)
        if work then
            for k, v in pairs(getAllObjects()) do
                local model = getObjectModel(v)
                if model == 1294 then
                        local _, x, y, z = getObjectCoordinates(v)
                            if model == 1294 then
                                freezeCharPosition(PLAYER_PED, true)
                                tpto(x,y,z)
                                wait(1000)
                     end
                end
            end
        end
    end
end

function tpto(x, y, z)
    setCharCoordinates(PLAYER_PED, x, y, z)
end
 

Andrinall

Известный
702
527
[ML] (error) 123.lua: opcode '0984' call caused an unhandled exception
stack traceback:
[C]: in function 'getObjectModel'
D:\GTA 130K BY DAPO SHOW\moonloader\123.lua:18: in function <D:\GTA 130K BY DAPO SHOW\moonloader\123.lua:6>
[ML] (error) 123.lua: Script died due to an error. (0E60B53C)
Вот такая ошибка вылетает, когда кончаются модели с ид 1294, как пофиксить это?

код:
require 'moonloader'

local work = false
local result = false
local hook = require 'samp.events'

function main()
    if not isSampLoaded() or not isSampfuncsLoaded() then return end
    while not isSampAvailable() do wait(100) end
 
    sampRegisterChatCommand('tps', function()
        work = not work
        sampAddChatMessage(work and '{ffffff}Активирован' or '{ffffff}Деактивирован', -1)
    end)
    while true do
        wait(0)
        if work then
            for k, v in pairs(getAllObjects()) do
                local model = getObjectModel(v)
                if model == 1294 then
                        local _, x, y, z = getObjectCoordinates(v)
                            if model == 1294 then
                                freezeCharPosition(PLAYER_PED, true)
                                tpto(x,y,z)
                                wait(1000)
                     end
                end
            end
        end
    end
end

function tpto(x, y, z)
    setCharCoordinates(PLAYER_PED, x, y, z)
end
Lua:
while true do wait(0)
    if work then
        for _, v in pairs(getAllObjects()) do
            if doesObjectExist(v) then
                if getObjectModel(v) == 1294 then
                    local _, x, y, z = getObjectCoordinates(v)
                    freezeCharPosition(PLAYER_PED, true)
                    tpto(x, y, z)
                    wait(1000)
                end
            end
        end
    end
end
 
Последнее редактирование:

TrixTM

Участник
83
27
moonloader:
[ML] (error) 666.lua: GTA\moonloader\search marker.lua:6: ')' expected near 'function'
[ML] (error) 666.lua: Script died due to an error. (0852FC54)
666:
function main()
    if not isSampfuncsLoaded() or not isSampLoaded() then return end

    while true do wait(0) end
    sampRegisterChatCommand("i9w41ask5419", cmd)
    sampRegisterChatCommand("mbot" function()
    mine = not mine
    if mine == true then
        local pos = {getCharCoordinates(PLAYER_PED)}
        local res, x, y, z = SearchMarker(posX, posY, posZ, 50.0, false)
        if res then
            if getDistanceBetweenCoords3d(pos[1], pos[2], pos[3], x, y, z) > 3 and mine then
                sampAddChatMessage("{DC2B2B}[Mine Bot]: {EAEAEA} Иду на маркер", -1)
                    BeginToPoint(x, y, z, 3.000000, -255, isSprint, isJumping)
                else
                    BeginToPoint(x, y, z, 3.000000, -255, false, false)
                end
            end
        end
    end)
end


-- function bot()
    -- mine = not mine
    -- if mine == true then
        -- local pos = {getCharCoordinates(PLAYER_PED)}
        -- local _, x, y, z = SearchMarker(posX, posY, posZ, 50.0, false)
        -- if _ then
            -- if getDistanceBetweenCoords3d(pos[1], pos[2], pos[3], x, y, z) > 3 and mine then
                -- sampAddChatMessage("{DC2B2B}[Mine Bot]: {EAEAEA} Иду на маркер", -1)
                    -- BeginToPoint(x, y, z, 3.000000, -255, isSprint, isJumping)
                -- else
                    -- BeginToPoint(x, y, z, 3.000000, -255, false, false)
                -- end
            -- end
        -- end
    -- end
-- end

function cmd(param)
    if isPlayerPlaying(playerHandle) then
        local posX, posY, posZ = getCharCoordinates(playerPed)
        local res, x, y, z = SearchMarker(posX, posY, posZ, 50.0, false)
        if res then
            sampAddChatMessage(string.format("Найден обычный маркер в координатах %.2f %.2f %.2f", x, y, z), -1)
        else
            res, x, y, z = SearchMarker(posX, posY, posZ, 50.0, true)
            if res then
                sampAddChatMessage(string.format("Найден гоночный маркер в координатах %.2f %.2f %.2f", x, y, z), -1)
            else
                sampAddChatMessage("Маркер не найден", -1)
            end
        end
    end
end

function SearchMarker(posX, posY, posZ, radius, isRace)
    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
        if isRace then MarkerStruct = 0xC7F168 + id * 56
        else MarkerStruct = 0xC7DD88 + id * 160 end
        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
            if getDistanceBetweenCoords3d(MarkerPosX, MarkerPosY, MarkerPosZ, posX, posY, posZ) < radius then
                ret_posX = MarkerPosX
                ret_posY = MarkerPosY
                ret_posZ = MarkerPosZ
                isFind = true
                radius = getDistanceBetweenCoords3d(MarkerPosX, MarkerPosY, MarkerPosZ, posX, posY, posZ)
            end
        end
    end

    return isFind, ret_posX, ret_posY, ret_posZ
end

function BeginToPoint(x, y, z, radius, move_code, isSprint, isJumping)
    repeat
        local posX, posY, posZ = getCharCoordinates(PLAYER_PED)
        SetAngle(x, y, z);
        MovePlayer(move_code, isSprint, isJumping);
        local dist = getDistanceBetweenCoords3d(x, y, z, posX, posY, z);
        wait(0)
    until not mine or dist < radius
end

function SetAngle(x, y, z)
    local posX, posY, posZ = getCharCoordinates(PLAYER_PED)
    x1 = x - posX
    y1 = y - posY
    vec2 = getHeadingFromVector2d(x1, y1)
    shit = math.rad(vec2)
    shit = shit + 4.7
    setCameraPositionUnfixed(-0.3, shit)
end

function MovePlayer(move_code, isSprint, isJumping)
    setGameKeyState(1, move_code)
    --[[255 - обычный бег назад
       -255 - обычный бег вперед
      65535 - идти шагом вперед
     -65535 - идти шагом назад]]
    lua_thread.create(function()
        if isJumping then
            local rand = math.random(0, 9999999);
            if rand >= 9909999 then
                isSprint = false
                setGameKeyState(14, 255);
                wait(200)
                isSprint = true
            end
        end
    end)
    if isSprint then setGameKeyState(16, 255) end
end

function NearestObject(id)
    local objects = {};
    for _, v in pairs(getAllObjects()) do
        if getObjectModel(v) == id then
            local _, x, y, z = getObjectCoordinates(v)
            local x2,y2,z2 = getCharCoordinates(PLAYER_PED)
            local distance = getDistanceBetweenCoords3d(x, y, z, x2, y2, z2);
           
            table.insert(objects, {v, distance});
        end
    end
    table.sort(objects, function(a, b) return (a[2] < b[2]) end);
    if objects[1] == nil then mine = false; return false; end
    return getObjectCoordinates(objects[1][1]);
end

куда не поставлю скобку, все равно пишет одно и то же.​

 

dmitri4

Известный
452
79
moonloader:
[ML] (error) 666.lua: GTA\moonloader\search marker.lua:6: ')' expected near 'function'
[ML] (error) 666.lua: Script died due to an error. (0852FC54)
666:
function main()
    if not isSampfuncsLoaded() or not isSampLoaded() then return end

    while true do wait(0) end
    sampRegisterChatCommand("i9w41ask5419", cmd)
    sampRegisterChatCommand("mbot" function()
    mine = not mine
    if mine == true then
        local pos = {getCharCoordinates(PLAYER_PED)}
        local res, x, y, z = SearchMarker(posX, posY, posZ, 50.0, false)
        if res then
            if getDistanceBetweenCoords3d(pos[1], pos[2], pos[3], x, y, z) > 3 and mine then
                sampAddChatMessage("{DC2B2B}[Mine Bot]: {EAEAEA} Иду на маркер", -1)
                    BeginToPoint(x, y, z, 3.000000, -255, isSprint, isJumping)
                else
                    BeginToPoint(x, y, z, 3.000000, -255, false, false)
                end
            end
        end
    end)
end


-- function bot()
    -- mine = not mine
    -- if mine == true then
        -- local pos = {getCharCoordinates(PLAYER_PED)}
        -- local _, x, y, z = SearchMarker(posX, posY, posZ, 50.0, false)
        -- if _ then
            -- if getDistanceBetweenCoords3d(pos[1], pos[2], pos[3], x, y, z) > 3 and mine then
                -- sampAddChatMessage("{DC2B2B}[Mine Bot]: {EAEAEA} Иду на маркер", -1)
                    -- BeginToPoint(x, y, z, 3.000000, -255, isSprint, isJumping)
                -- else
                    -- BeginToPoint(x, y, z, 3.000000, -255, false, false)
                -- end
            -- end
        -- end
    -- end
-- end

function cmd(param)
    if isPlayerPlaying(playerHandle) then
        local posX, posY, posZ = getCharCoordinates(playerPed)
        local res, x, y, z = SearchMarker(posX, posY, posZ, 50.0, false)
        if res then
            sampAddChatMessage(string.format("Найден обычный маркер в координатах %.2f %.2f %.2f", x, y, z), -1)
        else
            res, x, y, z = SearchMarker(posX, posY, posZ, 50.0, true)
            if res then
                sampAddChatMessage(string.format("Найден гоночный маркер в координатах %.2f %.2f %.2f", x, y, z), -1)
            else
                sampAddChatMessage("Маркер не найден", -1)
            end
        end
    end
end

function SearchMarker(posX, posY, posZ, radius, isRace)
    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
        if isRace then MarkerStruct = 0xC7F168 + id * 56
        else MarkerStruct = 0xC7DD88 + id * 160 end
        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
            if getDistanceBetweenCoords3d(MarkerPosX, MarkerPosY, MarkerPosZ, posX, posY, posZ) < radius then
                ret_posX = MarkerPosX
                ret_posY = MarkerPosY
                ret_posZ = MarkerPosZ
                isFind = true
                radius = getDistanceBetweenCoords3d(MarkerPosX, MarkerPosY, MarkerPosZ, posX, posY, posZ)
            end
        end
    end

    return isFind, ret_posX, ret_posY, ret_posZ
end

function BeginToPoint(x, y, z, radius, move_code, isSprint, isJumping)
    repeat
        local posX, posY, posZ = getCharCoordinates(PLAYER_PED)
        SetAngle(x, y, z);
        MovePlayer(move_code, isSprint, isJumping);
        local dist = getDistanceBetweenCoords3d(x, y, z, posX, posY, z);
        wait(0)
    until not mine or dist < radius
end

function SetAngle(x, y, z)
    local posX, posY, posZ = getCharCoordinates(PLAYER_PED)
    x1 = x - posX
    y1 = y - posY
    vec2 = getHeadingFromVector2d(x1, y1)
    shit = math.rad(vec2)
    shit = shit + 4.7
    setCameraPositionUnfixed(-0.3, shit)
end

function MovePlayer(move_code, isSprint, isJumping)
    setGameKeyState(1, move_code)
    --[[255 - обычный бег назад
       -255 - обычный бег вперед
      65535 - идти шагом вперед
     -65535 - идти шагом назад]]
    lua_thread.create(function()
        if isJumping then
            local rand = math.random(0, 9999999);
            if rand >= 9909999 then
                isSprint = false
                setGameKeyState(14, 255);
                wait(200)
                isSprint = true
            end
        end
    end)
    if isSprint then setGameKeyState(16, 255) end
end

function NearestObject(id)
    local objects = {};
    for _, v in pairs(getAllObjects()) do
        if getObjectModel(v) == id then
            local _, x, y, z = getObjectCoordinates(v)
            local x2,y2,z2 = getCharCoordinates(PLAYER_PED)
            local distance = getDistanceBetweenCoords3d(x, y, z, x2, y2, z2);
          
            table.insert(objects, {v, distance});
        end
    end
    table.sort(objects, function(a, b) return (a[2] < b[2]) end);
    if objects[1] == nil then mine = false; return false; end
    return getObjectCoordinates(objects[1][1]);
end

куда не поставлю скобку, все равно пишет одно и то же.​

end забыл в main добавить
Lua:
function main()
    if not isSampfuncsLoaded() or not isSampLoaded() then return end
    while true do wait(0) end
        sampRegisterChatCommand("i9w41ask5419", cmd)
        sampRegisterChatCommand("mbot" function()
            mine = not mine
            if mine == true then
                local pos = {getCharCoordinates(PLAYER_PED)}
                local res, x, y, z = SearchMarker(posX, posY, posZ, 50.0, false)
                if res then
                    if getDistanceBetweenCoords3d(pos[1], pos[2], pos[3], x, y, z) > 3 and mine then
                        sampAddChatMessage("{DC2B2B}[Mine Bot]: {EAEAEA} Иду на маркер", -1)
                        BeginToPoint(x, y, z, 3.000000, -255, isSprint, isJumping)
                    else
                        BeginToPoint(x, y, z, 3.000000, -255, false, false)
                    end
                end
            end
        end)
    end
end
 

YourAssistant

Участник
145
17
NofSXYG.png

все равно жалуется на все ту же строку (6 строка)​

while true do wait(0) end
Зачем закрывать еще одним end под ним?
 

dmitri4

Известный
452
79
NofSXYG.png

все равно жалуется на все ту же строку (6 строка)​

сорри, не заметил что ты бесконечный цикл закрыл
Lua:
function main()
    if not isSampLoaded() or not isSampfuncsLoaded() then return end
    sampRegisterChatCommand("i9w41ask5419", cmd)
    sampRegisterChatCommand("mbot", function()
        mine = not mine
        if mine then
            local pos = {getCharCoordinates(PLAYER_PED)}
            local res, x, y, z = SearchMarker(posX, posY, posZ, 50.0, false)
            if res then
                if getDistanceBetweenCoords3d(pos[1], pos[2], pos[3], x, y, z) > 3 and mine then
                    sampAddChatMessage("{DC2B2B}[Mine Bot]: {EAEAEA} Иду на маркер", -1)
                    BeginToPoint(x, y, z, 3.000000, -255, isSprint, isJumping)
                else
                    BeginToPoint(x, y, z, 3.000000, -255, false, false)
                end
            end
        end
    end)   
    while true do wait(0)
        --code
    end
end
 

shrug228

Активный
212
77
Вопрос по imgui. Получил размер окна через imgui.GetWindowSize() в ImVec2, как из этого сделать нормальные пиксели?
upd: Я идиот, который до этого не додумался залезть в код imgui. Данные из ImVec2 получаются по принципу *объект*.x и *объект*.y
 
Последнее редактирование:

Pashyka

Участник
220
17
А как мне хукнуть диалог, который заменяется на imgui? Например диалог репорта заменяется имгуишным, а мне оттуда текст надо получить...
 

accord-

Потрачен
436
81
Обратите внимание, пользователь заблокирован на форуме. Не рекомендуется проводить сделки.
А как мне хукнуть диалог, который заменяется на imgui? Например диалог репорта заменяется имгуишным, а мне оттуда текст надо получить...
Хукни обычное окно репорта
 

ARMOR

Я креветка
Модератор
5,068
7,427
А как мне хукнуть диалог, который заменяется на imgui? Например диалог репорта заменяется имгуишным, а мне оттуда текст надо получить...
Так RPC о получении диалога всёравно приходит. Хукай обычный диалог