Вопросы по 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
 
Последнее редактирование:

ollydbg

Известный
163
113
Как убрать текстдравы сервера, вот ид текстдравов "188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201".
Lua:
local sampev = require 'lib.samp.events'

function sampev.onShowTextDraw(id, data)
    if id == 188 or id == 189 or id == 190 or id == 191 or id == 192 or id == 193 or id == 194 or id == 195 or id == 196 or id == 197 or id == 198 or id == 199 or id == 200 or id == 201 then
        return false
    end
end
 
D

deleted-user-210352

Гость
Всем привет, подскажите пожалуйста как сместить скрипт справа снизу?
Я понимаю, что надо поменять позиции, на какие числа , подскажите ?
Хочу чтобы было тут Посмотреть вложение 113897
LUA:
positionX = 30
positionY = 120
попробуй убрать флаг imgui.WindowFlags.NoMove, и сможешь его закреплять в любом месте
вот тему еще нашел, мб поможет https://www.blast.hk/threads/38684/#post-594817
 

Fott

Простреленный
3,432
2,275
Lua:
local sampev = require 'lib.samp.events'

function sampev.onShowTextDraw(id, data)
    if id == 188 or id == 189 or id == 190 or id == 191 or id == 192 or id == 193 or id == 194 or id == 195 or id == 196 or id == 197 or id == 198 or id == 199 or id == 200 or id == 201 then
        return false
    end
end
Lua:
function Set(list)
  local set = {}
  for _, l in ipairs(list) do set[l] = true end
  return set
end

ids = Set{188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201}
function sampev.onShowTextDraw(id, data)
    if ids[id] then
        return false
    end
end
Так проще, не правда ли?
 
  • Нравится
  • Ха-ха
Реакции: abnomegd, kizn и ollydbg

Mr.Mastire222

Известный
529
259
Вот такая ошибка при подгрузки скрипта, вроде все библиотеки есть, можете сказать в чём проблема?
] (error) PD-Helper by Don Elino: ...s\dopsborka\GTA SA Samp RP\moonloader\PD-Helper (2).luac:0: attempt to index field 'targetVehicle' (a nil value)
stack traceback:
...s\dopsborka\GTA SA Samp RP\moonloader\PD-Helper (2).luac: in function <...s\dopsborka\GTA SA Samp RP\moonloader\PD-Helper (2).luac:0>
[ML] (error) PD-Helper by Don Elino: Script died due to an error. (1848531C)
 

kizn

О КУ)))
Всефорумный модератор
2,405
2,057
Lua:
function Set(list)
  local set = {}
  for _, l in ipairs(list) do set[l] = true end
  return set
end

ids = Set{188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201}
function sampev.onShowTextDraw(id, data)
    if ids[id] then
        return false
    end
end
Так проще, не правда ли?
Lua:
function onReceiveRpc(id, bs)
    if id == 134 then
        local tdId = raknetBitStreamReadInt16(bs)
        if tdId >= 188 and tdId <= 201 then
            return false
        end
    end
end
 

kizn

О КУ)))
Всефорумный модератор
2,405
2,057
Hello there everyone, I have been trying to make a script that autotypes a reaction for me.
For example, The server will show the following message:
First one to type DxG56(this one is random) gets money.
So, I want the script to find "DxG56" and send it. I tried this but it didn't work:



It's not working, It gives me a blank space in the chat.
?? try to find lua regular expressions patterns

%s - space characters
you need (.+)
 
  • Нравится
Реакции: Pu$$y

Dmitriy Makarov

25.05.2021
Проверенный
2,478
1,113
Как slider разделить на целые числа?
То есть не 1.222, а 1.2
Lua:
imgui.SliderFloat("Slider", slider, 0, 100, "%.0f") -- После %. указываешь, сколько цифр будет после запятой, в данном случае 0, т.е целые.
Код:
%.0f - 10
%.1f - 10.0
%.2f - 10.00
 
  • Нравится
Реакции: barjik

dinky

Участник
67
20
как рендерить на экране неполный квадрат(чтобы была только обводка, внутри обводи прозрачное)
 

abnomegd

Активный
335
35
Почему не работает, некоторые функции в диалоге /dialog 5
script:
require "lib.moonloader"
local color_dialog = 0xDEB887

-- Для диалога с ID 12
local dialogArr = {"История ников", "Добавить в записную книгу", "Показать документы", "Действия", "Имущество"};
local dialogStr = ""

for _, str in ipairs(dialogArr) do
    dialogStr = dialogStr .. str .. "\n"
end

-- Для диалога с ID 13
local dialogArra = {"Паспорт", "Лицензии", "Медкарта", "МедДиплом", "Выписка из тира", "Трудовая книга", "Военный билет", "Повестка в армию", "Отчёт о работе"}
local dialogStra = ""

for _, stra in ipairs(dialogArra) do
    dialogStra = dialogStra .. stra .. "\n"
end

-- Для диалога с ID 14
local dialogArras = {"Дать денег", "Передать металл / патроны / наркотики / маски / аптечки", "Подарить цветы", "Жениться", "Предложить побег", "Поселить к себе", "Дать ключи от авто"};
local dialogStras = ""

for _, stras in ipairs(dialogArras) do
    dialogStras = dialogStras .. stras .. "\n"
end

-- Для диалога с ID 15
local dialogArrasa = {"Продать машину", "Продать дом", "Продать бизнес", "Обмен авто", "Обмен домами", "Обмен бизнесами"};
local dialogStrasa = ""

for _, strasa in ipairs(dialogArrasa) do
    dialogStrasa = dialogStrasa .. strasa .. "\n"
end

function main()
    if not isSampLoaded() or not isSampfuncsLoaded() then return end
    while not isSampAvailable() do wait(100) end
    sampRegisterChatCommand("dialog", cmd_dialog)
    sampAddChatMessage("[HelperARP]: {FFFFFF}Скрипт загружен!", 0x5CBCFF)

    while true do
        wait(0)

        local valid, ped = getCharPlayerIsTargeting(PLAYER_HANDLE)
        if valid and doesCharExist(ped) then
            local result, id = sampGetPlayerIdByCharHandle(ped)
            if result and isKeyJustPressed(VK_X) then
                playerid = id
                cmd_dialog(2)
            end
        end

        local result, button, list, input = sampHasDialogRespond(11)
        if result then
            if button == 1 then
                if list == 0 then
                    sampSendChat("/pay "..tostring(playerid)..' '..input)
                end
            end
        end

        local result, button, list, input = sampHasDialogRespond(12)
        if result then
            if button == 1 then
                if list == 0 then -- "История ников"
                    sampSendChat("/history "..sampGetPlayerNickname(tonumber(playerid)))
                elseif list == 1 then -- "Добавить в записную книгу"
                    sampSendChat("/add "..tostring(playerid))
                elseif list == 2 then -- "Показать документы"
                    cmd_dialog(3)
                elseif list == 3 then -- "Действия"
                    cmd_dialog(4)
                elseif list == 4 then -- "Имущество"
                    cmd_dialog(5)
                end
            end
        end

        local result, button, list, input = sampHasDialogRespond(13)
        if result then
            if button == 1 then
                if list == 0 then -- Паспорт
                    lua_thread.create(function()
                        sampSendChat("/do Папка с документами в руке.")
                        wait(1000)
                        sampSendChat("/me достает из папки паспорт")
                        wait(1000)
                        sampSendChat("/pass "..tostring(playerid))
                    end)
                elseif list == 1 then -- Лицензии
                    lua_thread.create(function()
                        sampSendChat("/do Папка с документами в руке.")
                        wait(1000)
                        sampSendChat("/me достает из папки лицензии")
                        wait(1000)
                        sampSendChat("/lic "..tostring(playerid))
                    end)
                elseif list == 2 then -- Медкарта
                    lua_thread.create(function()
                        sampSendChat("/do Папка с документами в руке.")
                        wait(1000)
                        sampSendChat("/me достает из папки медицинскую карту")
                        wait(1000)
                        sampSendChat("/me показывает медицинскую карту человеку напротив")
                        wait(1000)
                        sampSendChat("/med "..tostring(playerid))
                    end)
                elseif list == 3 then -- МедДиплом
                    lua_thread.create(function()
                        sampSendChat("/do Папка с документами в руке.")
                        wait(1000)
                        sampSendChat("/me достает из папки медицинский диплом")
                        wait(1000)
                        sampSendChat("/me показывает медицинский диплом человеку напротив")
                    end)
                elseif list == 4 then -- Выписка из тира
                    lua_thread.create(function()
                        sampSendChat("/do Папка с документами в руке.")
                        wait(1000)
                        sampSendChat("/me достает из папки выписку из тира")
                        wait(1000)
                        sampSendChat("/skill "..tostring(playerid))
                    end)
                elseif list == 5 then -- Трудовая книга
                    lua_thread.create(function()
                        sampSendChat("/do Папка с документами в руке.")
                        wait(1000)
                        sampSendChat("/me достает из папки трудовую книгу")
                        wait(1000)
                        sampSendChat("/wbook "..tostring(playerid))
                    end)
                elseif list == 6 then -- Военный билет
                    lua_thread.create(function()
                        sampSendChat("/do Папка с документами в руке.")
                        wait(1000)
                        sampSendChat("/me достает из папки военный билет")
                        wait(1000)
                        sampSendChat("/me показывает военный билет человеку напротив")
                    end)
                elseif list == 7 then -- Повестка в армию
                    lua_thread.create(function()
                        sampSendChat("/do Папка с документами в руке.")
                        wait(1000)
                        sampSendChat("/me достает из папки повестку")
                        wait(1000)
                        sampSendChat("/me показывает повестку в армию человеку напротив")
                    end)
                elseif list == 8 then -- Отчёт о работе
                    lua_thread.create(function()
                        sampSendChat("/do Папка с документами в руке.")
                        wait(1000)
                        sampSendChat("/me достает из папки отчёт о выполненой работе")
                        wait(1000)
                        sampSendChat("/team "..tostring(playerid))
                    end)
                end
            else
            end
        end

        local result, button, list, input = sampHasDialogRespond(14)
        if result then
            if button == 1 then
                if list == 0 then -- Дать денег
                    cmd_dialog(1)
                elseif list == 1 then -- Передать
                    sampSendChat("/give "..tostring(playerid))
                elseif list == 2 then -- Подарить цветы
                    lua_thread.create(function()
                        sampSendChat("В знак моего внимания к Вам,")
                        wait(1000)
                        sampSendChat("Прошу принять этот подарок")
                        wait(1000)
                        sampSendChat("/do Букет цветов в руках.")
                        wait(1000)
                        sampSendChat("/me передает букет в руки")
                        wait(1000)
                        sampSendChat("/present "..tostring(playerid))
                    end)
                elseif list == 3 then -- Жениться на
                    sampSendChat("/wedding "..tostring(playerid))
                elseif list == 4 then -- Предложить побег
                    sampSendChat("/jailbreak "..tostring(playerid))
                elseif list == 5 then -- Поселить к себе
                    sampSendChat("/live "..tostring(playerid))
                elseif list == 6 then -- Дать ключи от авто
                    sampSendChat("/allow "..tostring(playerid))
                end
            else
            end
        end

        local result, button, list, input = sampHasDialogRespond(15)
        if result then
            if button == 1 then
                if list == 0 then -- Продать авто
                    cmd_dialog(6)
                elseif list == 1 then -- Передать дом
                    cmd_dialog(7)
                elseif list == 2 then -- Подарить бизнес
                    cmd_dialog(8)
                elseif list == 3 then -- Обмен авто
                    cmd_dialog(9)
                elseif list == 4 then -- Обмен домами
                    cmd_dialog(10)
                elseif list == 5 then -- Обмен бизнесами
                    cmd_dialog(11)
                end
            else
            end
        end

        local result, button, list, input = sampHasDialogRespond(16)
        if result then
            if button == 1 then
                if list == 0 then
                    sampSendChat("/sellmycar "..tostring(playerid).." "..input)
                end
            end
        end

        local result, button, list, input = sampHasDialogRespond(17)
        if result then
            if button == 1 then
                if list == 0 then
                    sampSendChat("/sellmyhome "..tostring(playerid).." "..input)
                end
            end
        end

        local result, button, list, input = sampHasDialogRespond(18)
        if result then
            if button == 1 then
                if list == 0 then
                    sampSendChat("/sellmybiz "..tostring(playerid).." "..input)
                end
            end
        end

        local result, button, list, input = sampHasDialogRespond(19)
        if result then
            if button == 1 then
                if list == 0 then
                    sampSendChat("/changecar "..tostring(playerid).." "..input)
                end
            end
        end

        local result, button, list, input = sampHasDialogRespond(20)
        if result then
            if button == 1 then
                if list == 0 then
                    sampSendChat("/changehome "..tostring(playerid).." "..input)
                end
            end
        end

        local result, button, list, input = sampHasDialogRespond(21)
        if result then
            if button == 1 then
                if list == 0 then
                    sampSendChat("/changebiz "..tostring(playerid).." "..input)
                end
            end
        end
    end
end

function cmd_dialog(arg)
    if tonumber(arg) == 1 then
        sampShowDialog(11, "{00ff00}Дать денег", "", "Дать", "Не дать", 1)
    elseif tonumber(arg) == 2 then
        sampShowDialog(12, "{00ff00}Меню взаимодействия с игроком", dialogStr, "Выбрать", "Закрыть", 2)
    elseif tonumber(arg) == 3 then
        sampShowDialog(13, "{00ff00}Показать документы", dialogStra, "Выбрать", "Закрыть", 2)
    elseif tonumber(arg) == 4 then
        sampShowDialog(14, "{00ff00}Действия", dialogStras, "Выбрать", "Закрыть", 2)
    elseif tonumber(arg) == 5 then
        sampShowDialog(15, "{00ff00}Имущество", dialogStrasa, "Выбрать", "Закрыть", 2)
    elseif tonumber(arg) == 6 then
        sampShowDialog(16, "{00ff00}Продать авто", "Введите цену машины.", "Выбрать", "Закрыть", 1)   
    elseif tonumber(arg) == 7 then
        sampShowDialog(17, "{00ff00}Продать дом", "Введите цену дома.", "Выбрать", "Закрыть", 1)   
    elseif tonumber(arg) == 8 then
        sampShowDialog(18, "{00ff00}Продать бизнес", "Введите цену бизнеса.", "Выбрать", "Закрыть", 1)
    elseif tonumber(arg) == 9 then
        sampShowDialog(19, "{00ff00}Обмен авто", "Введите столько сколько должен доплатить человек напротив.", "Выбрать", "Закрыть", 1)     
    elseif tonumber(arg) == 10 then
        sampShowDialog(20, "{00ff00}Обмен домами", "Введите столько сколько должен доплатить человек напротив.", "Выбрать", "Закрыть", 1)
    elseif tonumber(arg) == 11 then
        sampShowDialog(21, "{00ff00}Обмен бизнесами", "Введите столько сколько должен доплатить человек напротив.", "Выбрать", "Закрыть", 1)
    end
end
 

Fluffy1560

Активный
276
34
Господа, подскажите как сделать из этого нормальный цикл который будет делаться определенное количество раз
Lua:
function onSetSkin(playerId, SkinId) -- если поставлен скин рабочего то запускаем цикл тп по чекпоинтам(вот он и не работает)
  local botid = getBotId()
  if (playerId == botid) and (SkinId == 16) then
   defCallAdd(5000, false, function()
     local x = getCheckx()
     local y = getChecky()
     local z = getCheckz()
     coordMasterStart(x, y, z)
     for i = 1, 10 do
     function onCoordMasterComplete()
       local x = getCheckx()
       local y = getChecky()
       local z = getCheckz()
       i = i - 1
       printLog("Done: "..i)
       coordMasterStart(x, y, z)
end
end
end)
end
end
 

Sanchez.

Известный
704
187
Господа, подскажите как сделать из этого нормальный цикл который будет делаться определенное количество раз
Lua:
function onSetSkin(playerId, SkinId) -- если поставлен скин рабочего то запускаем цикл тп по чекпоинтам(вот он и не работает)
  local botid = getBotId()
  if (playerId == botid) and (SkinId == 16) then
   defCallAdd(5000, false, function()
     local x = getCheckx()
     local y = getChecky()
     local z = getCheckz()
     coordMasterStart(x, y, z)
     for i = 1, 10 do
     function onCoordMasterComplete()
       local x = getCheckx()
       local y = getChecky()
       local z = getCheckz()
       i = i - 1
       printLog("Done: "..i)
       coordMasterStart(x, y, z)
end
end
end)
end
end
Можно сделать циклом repeat
Lua:
repeat
    --code
until act ~= false
Это пример если что. Это будет повторяться, пока act не будет false
 

romacaddy

Известный
Проверенный
234
206
ссылку можешь дать?