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

Tomato

Активный
387
88
1606242004460.png


Помогите сделать примерно такое меню
 

chapo

🫡 В армии с 17.10.2023. В ЛС НЕ ОТВЕЧАЮ
Друг
8,768
11,210
почему это работает
Lua:
local d = imgui.ImFloat4(1, 1, 1, 1)

colors[clr.WindowBg]               = ImVec4(d.v[1], d.v[2], d.v[3], d.v[4])
а это нет?
Lua:
local colors = {
    text = imgui.ImFloat4(ini.text.r, ini.text.g, ini.text.b, ini.text.a),
    border = imgui.ImFloat4(ini.border.r, ini.border.g, ini.border.b, ini.border.a),
    back = imgui.ImFloat4(ini.back.r, ini.back.g, ini.back.b, ini.back.a),
    button = imgui.ImFloat4(ini.button.r, ini.button.g, ini.button.b, ini.button.a),
    button_hovered = imgui.ImFloat4(ini.button_hovered.r, ini.button_hovered.g, ini.button_hovered.b, ini.button_hovered.a),
    button_text = imgui.ImFloat4(ini.button_text.r, ini.button_text.g, ini.button_text.b, ini.button_text.a),
    inputs = imgui.ImFloat4(ini.inputs.r, ini.inputs.g, ini.inputs.b, ini.inputs.a),
}
Lua:
 colors[clr.Text]                   = ImVec4(colors.text.v[1], colors.text.v[2], colors.text.v[3], colors.text.v[4])
    colors[clr.WindowBg]               = ImVec4(colors.border.v[1], colors.border.v[2], colors.border.v[3], colors.border.v[4])
    colors[clr.ChildWindowBg]          = ImVec4(colors.back.v[1], colors.back.v[2], colors.back.v[3], colors.back.v[4])
    colors[clr.Button]                 = ImVec4(colors.button.v[1], colors.button.v[2], colors.button.v[3], colors.button.v[4])
    colors[clr.ButtonHovered]          = ImVec4(colors.button_hovered.v[1], colors.button_hovered.v[2], colors.button_hovered.v[3], colors.button_hovered.v[4])
    colors[clr.FrameBg]                = ImVec4(colors.inputs.v[1], colors.inputs.v[2], colors.inputs.v[3], colors.inputs.v[4])

Lua:
stack index -1, expected number, received string: not an integer
вопрос, схуяли int превратилось в string?
 

meowprd

Тот самый Котовский
Проверенный
1,280
712
как добавить таймер, после стоит в афк ?Посмотреть вложение 119388
использовать local startAfk = os.clock в тот момент, когда мы добавляем в переменную статус "Стоит в АФК" (или как там у тебя, не знаю)
и когда мы выводим текст "Стоит в АФК", то вычитать разность os.clock() - startAfk, это и будет время в секундах
 

barjik

Известный
464
190
Как получить id игроков в зоне стрима кроме моего?
Желательно полный код
 

Nicolas

Активный
114
66
Как получить id игроков в зоне стрима кроме моего?
Желательно полный код

id stream:
function main()
    sampRegisterChatCommand('stream',get_id)

    while true do wait(0)
    end
end

function get_id()
    for k, v in pairs(getAllChars()) do
        if doesCharExist(v) and v ~= PLAYER_PED then
            _, id = sampGetPlayerIdByCharHandle(v)
            if id ~= -1 then
                sampAddChatMessage(id,-1)
                naydeno = true
            end
        end
    end


    if not naydeno then
        sampAddChatMessage('В радиусе нет ни одного игрока!', -1)
    end

end
Активация по команде /stream
 
Последнее редактирование:
  • Нравится
Реакции: barjik

Corrygan228

Участник
132
9
В чём проблема? Не могу понять
[ML] (error) Gos-Helper.lua: ...ZONA GAMES\bin\Rodina(scripts)\moonloader\Gos-Helper.lua:320: attempt to call a string value
stack traceback:
...ZONA GAMES\bin\Rodina(scripts)\moonloader\Gos-Helper.lua: in function <...ZONA GAMES\bin\Rodina(scripts)\moonloader\Gos-Helper.lua:317>
[ML] (error) Gos-Helper.lua: Script died due to an error. (0952287C)
 

Вложения

  • Gos-Helper.lua
    18.9 KB · Просмотры: 4

Nicolas

Активный
114
66
В чём проблема? Не могу понять
[ML] (error) Gos-Helper.lua: ...ZONA GAMES\bin\Rodina(scripts)\moonloader\Gos-Helper.lua:320: attempt to call a string value
stack traceback:
...ZONA GAMES\bin\Rodina(scripts)\moonloader\Gos-Helper.lua: in function <...ZONA GAMES\bin\Rodina(scripts)\moonloader\Gos-Helper.lua:317>
[ML] (error) Gos-Helper.lua: Script died due to an error. (0952287C)
1635404074275.png
 

barjik

Известный
464
190
id stream:
function main()
    sampRegisterChatCommand('stream',get_id)

    while true do wait(0)
    end
end

function get_id()
    for k, v in pairs(getAllChars()) do
        if doesCharExist(v) and v ~= PLAYER_PED then
            _, id = sampGetPlayerIdByCharHandle(v)
            if id ~= -1 then
                sampAddChatMessage(id,-1)
                naydeno = true
            end
        end
    end


    if not naydeno then
        sampAddChatMessage('В радиусе нет ни одного игрока!', -1)
    end

end
Активация по команде /stream
Не понимаю как открыть поток, чтоб выводить сообщения с айдишниками c задержкой
code:
function get_id()
    for k, v in pairs(getAllChars()) do
        if doesCharExist(v) and v ~= PLAYER_PED then
            _, id = sampGetPlayerIdByCharHandle(v)
            if id ~= -1 then
            lua_thread.create(function()
           -- wait(1000)
            sampAddChatMessage(id, -1)
            naydeno = true
            end)
        end
    end
end

    if not naydeno then
        sampAddChatMessage('В радиусе нет ни одного игрока!', -1)
    end
end
 

Nicolas

Активный
114
66
Не понимаю как открыть поток, чтоб выводить сообщения с айдишниками c задержкой
code:
function get_id()
    for k, v in pairs(getAllChars()) do
        if doesCharExist(v) and v ~= PLAYER_PED then
            _, id = sampGetPlayerIdByCharHandle(v)
            if id ~= -1 then
            lua_thread.create(function()
           -- wait(1000)
            sampAddChatMessage(id, -1)
            naydeno = true
            end)
        end
    end
end

    if not naydeno then
        sampAddChatMessage('В радиусе нет ни одного игрока!', -1)
    end
end
stream:
function main()
    sampRegisterChatCommand('stream',get_id)
    while true do wait(0)
    end
end
function get_id()
    lua_thread.create(function()
        for k, v in pairs(getAllChars()) do 
            if doesCharExist(v) and v ~= PLAYER_PED then 
                _, id = sampGetPlayerIdByCharHandle(v)
                if id ~= -1 then
                    sampAddChatMessage(id,-1)
                    naydeno = true
                    wait(200)
                end
            end
        end
    
        if not naydeno then
            sampAddChatMessage('В радиусе нет ни одного игрока!', -1)
        end
    end)
end
 
  • Нравится
Реакции: barjik

Mr.Mastire222

Известный
529
259
Как сделать оверлей на imgui который будет выводит информацию о том, ID игрока который нанес урон и сколько урона
 

chapo

🫡 В армии с 17.10.2023. В ЛС НЕ ОТВЕЧАЮ
Друг
8,768
11,210
Как сделать оверлей на imgui который будет выводит информацию о том, ID игрока который нанес урон и сколько урона
Lua:
require 'lib.moonloader'
local weapons = require 'game.weapons'
local imgui = require('imgui')
local encoding = require 'encoding'
encoding.default = 'CP1251'
u8 = encoding.UTF8

local sampev = require 'lib.samp.events'

local window = imgui.ImBool(false)
local notf = imgui.ImBool(false)

local send = {
    playerId = 'N/A',
    weapon = 'N/A',
    damage = 'N/A',
    showtime = 10,
}

local inicfg = require 'inicfg'
local directIni = 'AutoDMReportByChapo.ini'
local ini = inicfg.load(inicfg.load({
    main = {
        enabled = true,
        minDamage = 15,
        sendText = 'DM - {name} [{id}]. Урон: {damage}, Оружие: {weapon}',
    },
}, directIni))
inicfg.save(ini, directIni)

local settings = {
    enabled = imgui.ImBool(ini.main.enabled),
    minDamage = imgui.ImInt(ini.main.minDamage),
    sendText = imgui.ImBuffer(u8(ini.main.sendText), 256)
    showtime = imgui.ImInt(ini.main.showtime),
}

function main()
    while not isSampAvailable() do wait(200) end
    if not sampGetCurrentServerName():find('Arizona') then
        thisScript():unload()
    end
    imgui.Process = false
    window.v = false  --show window
    sampRegisterChatCommand('dmrep', function()
        window.v = not window.v
    end)
    while true do
        wait(0)
        imgui.Process = window.v or notf.v
        if notf.v then
            if not sampIsCursorActive() then
                if wasKeyPressed(VK_F2) then
                    notf.v = false
                    send.state = false
                    sampSendChat('/report')
                end
                if wasKeyPressed(VK_F3) then
                    notf.v = false
                    send.state = false
                end
            end
            if wasKeyPressed(VK_LMENU) then
                sampToggleCursor(not sampIsCursorActive())
            end
        end
    end
end

function imgui.OnDrawFrame()
    if notf.v then
        local resX, resY = getScreenResolution()
        local sizeX, sizeY = 300, 155
        imgui.SetNextWindowPos(imgui.ImVec2(resX / 2 - sizeX / 2, resY - 100 - sizeY), imgui.Cond.FirstUseEver)
        imgui.SetNextWindowSize(imgui.ImVec2(sizeX, sizeY), imgui.Cond.FirstUseEver)
        imgui.Begin('Auto DM Report', notf, imgui.WindowFlags.NoResize + imgui.WindowFlags.NoTitleBar)

        imgui.ShowCursor = false

        imgui.CenterText(u8'Нанесен урон!')
        imgui.Separator()

        imgui.SetCursorPosX(5) imgui.Text(u8'Игрок: ') imgui.SameLine(100) imgui.Text(sampGetPlayerNickname(send.playerId)..' ( '..tostring(send.playerId)..')')
        imgui.SetCursorPosX(5) imgui.Text(u8'Оружие: ') imgui.SameLine(100) imgui.Text(send.weapon)
        imgui.SetCursorPosX(5) imgui.Text(u8'Урон: ') imgui.SameLine(100) imgui.Text(tostring(send.damage))

        imgui.Separator()
        imgui.CenterText(u8'Отправить репорт?')

        imgui.SetCursorPosX(5)
        if imgui.Button(u8'Отправить (F2)', imgui.ImVec2(sizeX / 2 - 10, 20)) then
            notf.v = true
            send.state = true
            sampSendChat('/report')
        end
        imgui.SameLine()
        imgui.SetCursorPosX(5 + sizeX / 2 - 15 + 13)
        if imgui.Button(u8'Закрыть (F3)', imgui.ImVec2(sizeX / 2 - 10, 20)) then
            notf.v = false
            send.state = false
        end
        imgui.CenterText(u8'Нажмите левый ALT что бы включить курсор')
        imgui.End()
    end
    if window.v then
        local resX, resY = getScreenResolution()
        local sizeX, sizeY = 400, 135
        imgui.SetNextWindowPos(imgui.ImVec2(resX / 2 - sizeX / 2, resY / 2 - sizeY / 2), imgui.Cond.FirstUseEver)
        imgui.SetNextWindowSize(imgui.ImVec2(sizeX, sizeY), imgui.Cond.FirstUseEver)
        imgui.Begin('Auto DM Report by chapo', window)

        imgui.Checkbox(u8'Отправлять репорт при получении урона', settings.enabled)
        imgui.CenterText(u8'Минимальный урон для репорта: ')
        
        imgui.PushItemWidth(sizeX - 10)
        imgui.SetCursorPosX(5)
        imgui.InputInt('##minDamage', settings.minDamage)

        imgui.CenterText(u8'Текст для отправки в репорт: ')
        imgui.SameLine()
        imgui.TextDisabled(u8'?')
        if imgui.IsItemHovered() then
            imgui.BeginTooltip()
                imgui.Text(u8'{id} - id игрока\n{name} - заменяется на ник игрока\n{damage} - нанесенный урон\n{weapon} - название оружия из которого был нанесен урон')
            imgui.EndTooltip()
        end
        
        imgui.SetCursorPosX(5)
        imgui.InputText('##sendText', settings.sendText)


        imgui.CenterText(u8'Время отображения окна (секунды): ')
        imgui.SetCursorPosX(5)
        imgui.InputInt('##showtime', settings.showtime)

        imgui.PopItemWidth()
        imgui.End()
    end
end

function sampev.onSendTakeDamage(playerId, damage, weapon, bodypart)
    if playerId ~= 65535 then
        if damage >= settings.minDamage.v then
            send.playerId = playerId
            send.damage = damage
            send.weapon = weapons.get_name(id)
            send.weaponId = weapon
            notf.v = true
            lua_thread.create(function()
                wait(settings.win_show_time.v * 1000)
                notf.v = false
                send.state = false
            end)
        end
    end
end

function sampev.onShowDialog(id, style, title, button1, button2, text)
    if id == 32 then
        if send.state then
            local text = settings.sendText.v
            local text = text:gsub('{id}', send.playerId)
            local text = text:gsub('{name}', sampGetPlayerNickname(send.playerId))
            local text = text:gsub('{weapon}', send.weapon)
            local text = text:gsub('{damage}', send.damage)

            sampSendDialogResponse(32, 1, nil, u8(text))
            send.state = false
            return false
        end
    end
end

function imgui.Link(link, text)
    text = text or link
    local tSize = imgui.CalcTextSize(text)
    local p = imgui.GetCursorScreenPos()
    local DL = imgui.GetWindowDrawList()
    local col = { 0xFFFF7700, 0xFFFF9900 }
    if imgui.InvisibleButton("##" .. link, tSize) then os.execute("explorer " .. link) end
    local color = imgui.IsItemHovered() and col[1] or col[2]
    DL:AddText(p, color, text)
    DL:AddLine(imgui.ImVec2(p.x, p.y + tSize.y), imgui.ImVec2(p.x + tSize.x, p.y + tSize.y), color)
end

function imgui.CenterText(text)
    imgui.SetCursorPosX(imgui.GetWindowSize().x / 2 - imgui.CalcTextSize(text).x / 2)
    imgui.Text(text)
end

function BH_theme()
    imgui.SwitchContext()
    local style = imgui.GetStyle()
    local colors = style.Colors
    local clr = imgui.Col
    local ImVec4 = imgui.ImVec4
    local ImVec2 = imgui.ImVec2
 
    style.WindowPadding = ImVec2(6, 4)
    style.WindowRounding = 5.0
    style.ChildWindowRounding = 5.0
    style.FramePadding = ImVec2(5, 2)
    style.FrameRounding = 5.0
    style.ItemSpacing = ImVec2(7, 5)
    style.ItemInnerSpacing = ImVec2(1, 1)
    style.TouchExtraPadding = ImVec2(0, 0)
    style.IndentSpacing = 6.0
    style.ScrollbarSize = 12.0
    style.ScrollbarRounding = 5.0
    style.GrabMinSize = 20.0
    style.GrabRounding = 2.0
    style.WindowTitleAlign = ImVec2(0.5, 0.5)

    colors[clr.Text]                   = ImVec4(1.00, 1.00, 1.00, 1.00)
    colors[clr.TextDisabled]           = ImVec4(0.28, 0.30, 0.35, 1.00)
    colors[clr.WindowBg]               = ImVec4(0.16, 0.18, 0.22, 1.00)
    colors[clr.ChildWindowBg]          = ImVec4(0.19, 0.22, 0.26, 1)
    colors[clr.PopupBg]                = ImVec4(0.05, 0.05, 0.10, 0.90)
    colors[clr.Border]                 = ImVec4(0.19, 0.22, 0.26, 1.00)
    colors[clr.BorderShadow]           = ImVec4(0.00, 0.00, 0.00, 0.00)
    colors[clr.FrameBg]                = ImVec4(0.19, 0.22, 0.26, 1.00)
    colors[clr.FrameBgHovered]         = ImVec4(0.22, 0.25, 0.30, 1.00)
    colors[clr.FrameBgActive]          = ImVec4(0.22, 0.25, 0.29, 1.00)
    colors[clr.TitleBg]                = ImVec4(0.19, 0.22, 0.26, 1.00)
    colors[clr.TitleBgActive]          = ImVec4(0.19, 0.22, 0.26, 1.00)
    colors[clr.TitleBgCollapsed]       = ImVec4(0.19, 0.22, 0.26, 0.59)
    colors[clr.MenuBarBg]              = ImVec4(0.19, 0.22, 0.26, 1.00)
    colors[clr.ScrollbarBg]            = ImVec4(0.20, 0.25, 0.30, 0.60)
    colors[clr.ScrollbarGrab]          = ImVec4(0.41, 0.55, 0.78, 1.00)
    colors[clr.ScrollbarGrabHovered]   = ImVec4(0.49, 0.63, 0.86, 1.00)
    colors[clr.ScrollbarGrabActive]    = ImVec4(0.49, 0.63, 0.86, 1.00)
    colors[clr.ComboBg]                = ImVec4(0.20, 0.20, 0.20, 0.99)
    colors[clr.CheckMark]              = ImVec4(0.90, 0.90, 0.90, 0.50)
    colors[clr.SliderGrab]             = ImVec4(1.00, 1.00, 1.00, 0.30)
    colors[clr.SliderGrabActive]       = ImVec4(0.80, 0.50, 0.50, 1.00)
    colors[clr.Button]                 = ImVec4(0.41, 0.55, 0.78, 1.00)
    colors[clr.ButtonHovered]          = ImVec4(0.49, 0.62, 0.85, 1.00)
    colors[clr.ButtonActive]           = ImVec4(0.49, 0.62, 0.85, 1.00)
    colors[clr.Header]                 = ImVec4(0.19, 0.22, 0.26, 1.00)
    colors[clr.HeaderHovered]          = ImVec4(0.22, 0.24, 0.28, 1.00)
    colors[clr.HeaderActive]           = ImVec4(0.22, 0.24, 0.28, 1.00)
    colors[clr.Separator]              = ImVec4(0.41, 0.55, 0.78, 1.00)
    colors[clr.SeparatorHovered]       = ImVec4(0.41, 0.55, 0.78, 1.00)
    colors[clr.SeparatorActive]        = ImVec4(0.41, 0.55, 0.78, 1.00)
    colors[clr.ResizeGrip]             = ImVec4(0.41, 0.55, 0.78, 1.00)
    colors[clr.ResizeGripHovered]      = ImVec4(0.49, 0.61, 0.83, 1.00)
    colors[clr.ResizeGripActive]       = ImVec4(0.49, 0.62, 0.83, 1.00)
    colors[clr.CloseButton]            = ImVec4(0.41, 0.55, 0.78, 1.00)
    colors[clr.CloseButtonHovered]     = ImVec4(0.50, 0.63, 0.84, 1.00)
    colors[clr.CloseButtonActive]      = ImVec4(0.41, 0.55, 0.78, 1.00)
    colors[clr.PlotLines]              = ImVec4(1.00, 1.00, 1.00, 1.00)
    colors[clr.PlotLinesHovered]       = ImVec4(0.90, 0.70, 0.00, 1.00)
    colors[clr.PlotHistogram]          = ImVec4(0.90, 0.70, 0.00, 1.00)
    colors[clr.PlotHistogramHovered]   = ImVec4(1.00, 0.60, 0.00, 1.00)
    colors[clr.TextSelectedBg]         = ImVec4(0.41, 0.55, 0.78, 1.00)
    colors[clr.ModalWindowDarkening]   = ImVec4(0.16, 0.18, 0.22, 0.76)
end
BH_theme()
 
  • Нравится
Реакции: Mr.Mastire222

SurnikSur

Активный
284
40
Привет, у меня есть координаты эмулировать нажать Wпока я не приеду на метку. как это можно сделать ?
 

СоМиК

Известный
457
310
qq, глупый вопрос, как полностью остановить игрока в луа, чтобы даже клавиши не работали, чтобы как при прогрузке текстур на серверах было, когда игрок фризится на пару секунд после входа в инту, ОБЯЗАТЕЛЬНО, ранее зажатые клавиши должны отжиматься
 
Последнее редактирование: