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

Di3

Участник
432
20
Кто может помочь решить проблему. Сделал функцию проверки Регов, пишу /getip ID в чате скрипт видит строку > Включает функцию проверки,все проверяется хорошо. Аналогичное делаю через sampSendChat('/getip '..rInfo.id) по нажатию клавиши на клавиатуре , все также проверяется хорошо и никаких проблем нет. Но когда это же делаю через imgui , функция так же выполняется,реги выводятся,но через 5-10 секунд после вывода происходит краш,не понимаю в чем проблема,в логе ничего нет,просто выгружаются все скрипты и гта закрывается.
Кнопка
Lua:
    imgui.SetCursorPos(imgui.ImVec2(133, 6))
        if imgui.Button(u8'Реги') then
            sampSendChat('/getip '..rInfo.id)
        end
Последующее выполнение функции когда найден нужный ответ после команды выше
Lua:
function  SE.onServerMessage(collor,msg)
if msg:find('Nik %[(.*)%]  R%-IP %[(.*)%]  IP %[(.*)%]')  then
local reginick,regip,reglast =    msg:match('Nik %[(.*)%]  R%-IP %[(.*)%]  IP %[(.*)%]')
if statusproverkiregov == false then
        lua_thread.create(function()
            regdanny = {}
checkregi(regip,reglast)
while not regdanny[1] or not regdanny[4] do    wait(0) if statuserrorregi == true then statuserrorregi = false return end end
sampAddChatMessage(reginick, -1)
sampAddChatMessage(regdanny[1]..'  '..regdanny[4], -1)
sampAddChatMessage(regdanny[2]..'   '..regdanny[5], -1)
sampAddChatMessage(regdanny[3]..'   '..regdanny[6], -1)
end)
else
    sampAddChatMessage('[Ошибка] Я не могу работать так быстро!', 0xFF44FF)
end
end
end

Сама функция
Lua:
  function checkregi(ipregdanny,iplastregdanny)
        statusproverkiregov=true
                    regdanny = {}
                    regdanny2 = {}
                  lua_thread.create(function()
        async_http_request("GET", 'http://ip-api.com/json/'..u8(ipregdanny)..'?lang=ru' , nil,
        function(response)
        local regvrem = u8:decode(response.text)
    local city4,country3,isp2 = regvrem:match('city%":%"(.*)%"%,%"country%":%"(.*)%"%,%"countryCode.*%,%"isp%":%"(.*)%"%,%"lat"')
regdanny = {country3,city4,isp2}
        end,
        function(err)
        sampAddChatMessage('[ERROR] Не удалось загрузить информацию о IP REG',0xFF0000)
            regdanny[1] = 'ERROR'
        end)
        async_http_request("GET", 'http://ip-api.com/json/'..u8(iplastregdanny)..'?lang=ru' , nil,
        function(response)
        local regvrem = u8:decode(response.text)
        local city1,country1,isp1 = regvrem:match('city%":%"(.*)%"%,%"country%":%"(.*)%"%,%"countryCode.*%,%"isp%":%"(.*)%"%,%"lat"')
    regdanny2 = {country1,city1,isp1}
        end,
        function(err)
        sampAddChatMessage('[ERROR] Не удалось загрузить информацию о IP LAST',0xFF0000)
        regdanny2[1] = 'ERROR'
        end)
while not regdanny2[1] or not regdanny[1] do wait(0) end
if not regdanny2[1]:find('ERROR') or not regdanny[1]:find('ERROR') then
regdanny = {regdanny[1],regdanny[2],regdanny[3],regdanny2[1],regdanny2[2],regdanny2[3]}
wait(500)
statusproverkiregov=false
else
        sampAddChatMessage('[Ошибка] Произошла ошибка проверки. Повторите попытку.', -1)
        statuserrorregi = true
end
end)
end
 

offsya

Новичок
12
0
Как зашифровать код от всякой нечисти и шкилы? Чтобы не воровали работу
 

tlwsn

Известный
537
85
как можно отследить то, что я поставил метку? Что бы когда поставил метку на карте скрипт сразу ТПхал?
 

doradojka

Новичок
20
0
Почему при вводе /cg(открытие диалога) выдает unknown command? Хотелось бы узнать, что именно руинит это все и почему. Ошибок нет никаких в логе, если что.
Lua:
local sampev = require 'lib.samp.events'
local encoding = require 'encoding'
local sf = require 'sampfuncs'
encoding.default = 'cp1251'
u8 = encoding.UTF8
isMenu = false

dialogMenu = {
    {
        title = u8:decode('Бита - 10 деталей'),
        onclick = function()
            funcGUN()
            sampSetCurrentDialogListItem(6)
            sampCloseCurrentDialogWithButton(1)
        end
    }
}

function main()
if not isSampLoaded() or not isSampfuncsLoaded() then return end
    while not isSampAvailable() do wait(0) end

    sampRegisterChatCommand("cg", function()
        isMenu = not isMenu
    end)

    while true do
        wait(0)
        if isMenu then
            submenus_show(dialogMenu, "Menu", "Choose", "Close", "Back")
            isMenu = false
        end
    end
end

function main()
    if not isSampfuncsLoaded() or not isSampLoaded() then return end
        while not isSampAvailable() do wait(0) end
    sampRegisterChatCommand('gun', funcGUN)
    sampRegisterChatCommand('patr', funcPATR)
end

function funcGUN()
    lua_thread.create(function()
        sampSendChat('/invex')
        wait(500)
        local i = 0
        for item in sampGetDialogText():gmatch("[^\r\n]+") do
            i = i+1
            if item:find(u8:decode("Оружейная деталь .+%[%d+.+%]")) ~= nil then
                sampSetCurrentDialogListItem(i-1)
                sampCloseCurrentDialogWithButton(1)
                wait(500)
                sampSetCurrentDialogListItem(5)
                sampCloseCurrentDialogWithButton(1)
            end
        end
    end)
end

function funcPATR()
    lua_thread.create(function()
        sampSendChat('/invex')
        wait(1000)
        local k = 0
        for item in sampGetDialogText():gmatch("[^\r\n]+") do
            k = k+1
            if item:find(u8:decode("Оружейная деталь .+%[%d+.+%]")) ~= nil then
                sampSetCurrentDialogListItem(k-1)
                sampCloseCurrentDialogWithButton(1)
                wait(500)
                sampSetCurrentDialogListItem(6)
                sampCloseCurrentDialogWithButton(1)
            end
        end
    end)
end

function submenus_show(menu, caption, select_button, close_button, back_button)
    select_button, close_button, back_button = select_button or 'Select', close_button or 'Close', back_button or 'Back'
    prev_menus = {}
    function display(menu, id, caption)
        local string_list = {}
        for i, v in ipairs(menu) do
            table.insert(string_list, type(v.submenu) == 'table' and v.title .. '  >>' or v.title)
        end
        sampShowDialog(id, caption, table.concat(string_list, '\n'), select_button, (#prev_menus > 0) and back_button or close_button, sf.DIALOG_STYLE_LIST)
        repeat
            wait(0)
            local result, button, list = sampHasDialogRespond(id)
            if result then
                if button == 1 and list ~= -1 then
                    local item = menu[list + 1]
                    if type(item.submenu) == 'table' then -- submenu
                        table.insert(prev_menus, {menu = menu, caption = caption})
                        if type(item.onclick) == 'function' then
                            item.onclick(menu, list + 1, item.submenu)
                        end
                        return display(item.submenu, id + 1, item.submenu.title and item.submenu.title or item.title)
                    elseif type(item.onclick) == 'function' then
                        local result = item.onclick(menu, list + 1)
                        if not result then return result end
                        return display(menu, id, caption)
                    end
                else -- if button == 0
                    if #prev_menus > 0 then
                        local prev_menu = prev_menus[#prev_menus]
                        prev_menus[#prev_menus] = nil
                        return display(prev_menu.menu, id - 1, prev_menu.caption)
                    end
                    return false
                end
            end
        until result
    end
    return display(menu, 31337, caption or menu.title)
end
 

trefa

Известный
Всефорумный модератор
2,097
1,233
Почему при вводе /cg(открытие диалога) выдает unknown command? Хотелось бы узнать, что именно руинит это все и почему. Ошибок нет никаких в логе, если что.
Lua:
local sampev = require 'lib.samp.events'
local encoding = require 'encoding'
local sf = require 'sampfuncs'
encoding.default = 'cp1251'
u8 = encoding.UTF8
isMenu = false

dialogMenu = {
    {
        title = u8:decode('Бита - 10 деталей'),
        onclick = function()
            funcGUN()
            sampSetCurrentDialogListItem(6)
            sampCloseCurrentDialogWithButton(1)
        end
    }
}

function main()
if not isSampLoaded() or not isSampfuncsLoaded() then return end
    while not isSampAvailable() do wait(0) end

    sampRegisterChatCommand("cg", function()
        isMenu = not isMenu
    end)

    while true do
        wait(0)
        if isMenu then
            submenus_show(dialogMenu, "Menu", "Choose", "Close", "Back")
            isMenu = false
        end
    end
end

function main()
    if not isSampfuncsLoaded() or not isSampLoaded() then return end
        while not isSampAvailable() do wait(0) end
    sampRegisterChatCommand('gun', funcGUN)
    sampRegisterChatCommand('patr', funcPATR)
end

function funcGUN()
    lua_thread.create(function()
        sampSendChat('/invex')
        wait(500)
        local i = 0
        for item in sampGetDialogText():gmatch("[^\r\n]+") do
            i = i+1
            if item:find(u8:decode("Оружейная деталь .+%[%d+.+%]")) ~= nil then
                sampSetCurrentDialogListItem(i-1)
                sampCloseCurrentDialogWithButton(1)
                wait(500)
                sampSetCurrentDialogListItem(5)
                sampCloseCurrentDialogWithButton(1)
            end
        end
    end)
end

function funcPATR()
    lua_thread.create(function()
        sampSendChat('/invex')
        wait(1000)
        local k = 0
        for item in sampGetDialogText():gmatch("[^\r\n]+") do
            k = k+1
            if item:find(u8:decode("Оружейная деталь .+%[%d+.+%]")) ~= nil then
                sampSetCurrentDialogListItem(k-1)
                sampCloseCurrentDialogWithButton(1)
                wait(500)
                sampSetCurrentDialogListItem(6)
                sampCloseCurrentDialogWithButton(1)
            end
        end
    end)
end

function submenus_show(menu, caption, select_button, close_button, back_button)
    select_button, close_button, back_button = select_button or 'Select', close_button or 'Close', back_button or 'Back'
    prev_menus = {}
    function display(menu, id, caption)
        local string_list = {}
        for i, v in ipairs(menu) do
            table.insert(string_list, type(v.submenu) == 'table' and v.title .. '  >>' or v.title)
        end
        sampShowDialog(id, caption, table.concat(string_list, '\n'), select_button, (#prev_menus > 0) and back_button or close_button, sf.DIALOG_STYLE_LIST)
        repeat
            wait(0)
            local result, button, list = sampHasDialogRespond(id)
            if result then
                if button == 1 and list ~= -1 then
                    local item = menu[list + 1]
                    if type(item.submenu) == 'table' then -- submenu
                        table.insert(prev_menus, {menu = menu, caption = caption})
                        if type(item.onclick) == 'function' then
                            item.onclick(menu, list + 1, item.submenu)
                        end
                        return display(item.submenu, id + 1, item.submenu.title and item.submenu.title or item.title)
                    elseif type(item.onclick) == 'function' then
                        local result = item.onclick(menu, list + 1)
                        if not result then return result end
                        return display(menu, id, caption)
                    end
                else -- if button == 0
                    if #prev_menus > 0 then
                        local prev_menu = prev_menus[#prev_menus]
                        prev_menus[#prev_menus] = nil
                        return display(prev_menu.menu, id - 1, prev_menu.caption)
                    end
                    return false
                end
            end
        until result
    end
    return display(menu, 31337, caption or menu.title)
end
У тебя 2 майна :facepalm2:

как сделать таймер с миллисекундами до тысячных
Вместо os.time() использовать os.clock()
 

offsya

Новичок
12
0
Есть ли какие то туториалы на форуме по компилированию кода, чтобы нечисть не с3.14здила?
 

xISRAPILx

Перепишу свою жизнь на PHP
Проверенный
247
165
Всё ещё актуально. Не хочет нажимать на кнопки.

Lua:
    if string.find(text, "PED_FIREWEAPON") then
        if isKeyDown(0x02) then
            setVirtualKeyDown(0x02, false)
        end

        setVirtualKeyDown(0x01, true)
    elseif string.find(text, "PED_LOCK_TARGET") then
        if isKeyDown(0x01) then
            setVirtualKeyDown(0x01, false)
        end
    
        setVirtualKeyDown(0x02, true)
    end
 

Natami

Участник
377
26
Всё ещё актуально. Не хочет нажимать на кнопки.

Lua:
    if string.find(text, "PED_FIREWEAPON") then
        if isKeyDown(0x02) then
            setVirtualKeyDown(0x02, false)
        end

        setVirtualKeyDown(0x01, true)
    elseif string.find(text, "PED_LOCK_TARGET") then
        if isKeyDown(0x01) then
            setVirtualKeyDown(0x01, false)
        end
   
        setVirtualKeyDown(0x02, true)
    end
че ты пытаешься написать? скрин