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

meowkins

Новичок
26
0
Lua:
local colors = {
    0xAA000000,
    0xAAFF0000,
    0xAA0000FF
}
local index = 1

while true do wait(0)
    renderDrawBoxWithBorder(mainIni.pos.x + 40, mainIni.pos.y, 210, 105, colors[index], 1, 0xAA000000)
end

if imgui.Button('button') then
    if index > 2 then
        index = 1
    else
        index = index + 1
    end
end
Может я делаю что то не так, но скрипт говорит мне что я сравниваю index с нулем в имгуи кнопке.
Кнопку я поместил в function imgui.OnDrawFrame()
Таблицу с цветами после прогрузки сампа в мейне.
 

ROBERT PUSHER

Известный
305
212
Как игнорировать ~y~ и ~w~?
1624189139583.png
 
Обратите внимание, пользователь заблокирован на форуме. Не рекомендуется проводить сделки.
Подскажите пж как изменить уровень локальному игроку т.е себе же, вот например ник ставится через sampSetLocalPlayerName , как можно указать себе уровень , чтобы в табе был другой, и можно еще для изменения пинга
 

W1ll04eison

Участник
328
19
Подскажите, вот допустим я создал 5 пунктов, все 5 удалил, после перезагрузки скрипта все равно 1 пункт остается, подскажите, как это исправить:
Lua:
local directIniii = "moonloader\\SF_FBI\\config\\fast.ini"

if not doesFileExist("moonloader\\SF_FBI\\config\\fast.ini") then
    file = io.open("moonloader\\SF_FBI\\config\\fast.ini", "a")
    file:close()
end

local mainIniii = inicfg.load({}, directIniii)

local slots = 0

function imgui.TextColoredRGB(text, render_text) -- нужное (наверно)
    local max_float = imgui.GetWindowWidth()
    local style = imgui.GetStyle()
    local colors = style.Colors
    local ImVec4 = imgui.ImVec4

    local explode_argb = function(argb)
        local a = bit.band(bit.rshift(argb, 24), 0xFF)
        local r = bit.band(bit.rshift(argb, 16), 0xFF)
        local g = bit.band(bit.rshift(argb, 8), 0xFF)
        local b = bit.band(argb, 0xFF)
        return a, r, g, b
    end

    local getcolor = function(color)
        if color:sub(1, 6):upper() == 'SSSSSS' then
            local r, g, b = colors[1].x, colors[1].y, colors[1].z
            local a = tonumber(color:sub(7, 8), 16) or colors[1].w * 255
            return ImVec4(r, g, b, a / 255)
        end
        local color = type(color) == 'string' and tonumber(color, 16) or color
        if type(color) ~= 'number' then return end
        local r, g, b, a = explode_argb(color)
        return imgui.ImColor(r, g, b, a):GetVec4()
    end

    local render_text = function(text_)
        for w in text_:gmatch('[^\r\n]+') do
            local text, colors_, m = {}, {}, 1
            w = w:gsub('{(......)}', '{%1FF}')
            while w:find('{........}') do
                local n, k = w:find('{........}')
                local color = getcolor(w:sub(n + 1, k - 1))
                if color then
                    text[#text], text[#text + 1] = w:sub(m, n - 1), w:sub(k + 1, #w)
                    colors_[#colors_ + 1] = color
                    m = n
                end
                w = w:sub(1, n - 1) .. w:sub(k + 1, #w)
            end

            local length = imgui.CalcTextSize(w)
            if render_text == 2 then
                imgui.NewLine()
                imgui.SameLine(max_float / 2 - ( length.x / 2 ))
            elseif render_text == 3 then
                imgui.NewLine()
                imgui.SameLine(max_float - length.x - 5 )
            end
            if text[0] then
                for i = 0, #text do
                    imgui.TextColored(colors_[i] or colors[1], text[i])
                    imgui.SameLine(nil, 0)
                end
                imgui.NewLine()
            else imgui.Text(w) end


        end
    end

    render_text(text)
end

function explode_argb(argb)
    local a = bit.band(bit.rshift(argb, 24), 0xFF)
    local r = bit.band(bit.rshift(argb, 16), 0xFF)
    local g = bit.band(bit.rshift(argb, 8), 0xFF)
    local b = bit.band(argb, 0xFF)
    return a, r, g, b
end


function imgui.TextColoredRGB(text, render_text)
    local style = imgui.GetStyle()
    local colors = style.Colors
    local clr = imgui.Col
    local u8 = require 'encoding'.UTF8

    local explode_argb = function(argb)
        local a = bit.band(bit.rshift(argb, 24), 0xFF)
        local r = bit.band(bit.rshift(argb, 16), 0xFF)
        local g = bit.band(bit.rshift(argb, 8), 0xFF)
        local b = bit.band(argb, 0xFF)
        return a, r, g, b
    end

    local getcolor = function(color)
        if color:sub(1, 6):upper() == 'SSSSSS' then
            local r, g, b = colors[1].x, colors[1].y, colors[1].z
            local a = tonumber(color:sub(7, 8), 16) or colors[1].w * 255
            return ImVec4(r, g, b, a / 255)
        end
        local color = type(color) == 'string' and tonumber(color, 16) or color
        if type(color) ~= 'number' then return end
        local r, g, b, a = explode_argb(color)
        return imgui.ImColor(r, g, b, a):GetVec4()
    end

    local function render_text(text)
        for w in text:gmatch('[^\r\n]+') do
            local text, color = {}, {}
            local m = 1
            w = w:gsub('{(......)}', '{%1FF}')
            while w:find('{........}') do
                local n, k = w:find('{........}')
                local color = getcolor(w:sub(n + 1, k - 1))
                if color then
                    text[#text], text[#text + 1] = w:sub(m, n - 1), w:sub(k + 1, #w)
                    colors_[#colors_ + 1] = color
                    m = n
                end
                w = w:sub(1, n - 1) .. w:sub(k + 1, #w)
            end
            local length = imgui.CalcTextSize(u8(w))
            if render_text == 2 then
                imgui.NewLine()
                imgui.SameLine(max_float / 2 - ( length.x / 2 ))
            elseif render_text == 3 then
                imgui.NewLine()
                imgui.SameLine(max_float - length.x - 5 )
            end
            if text[0] then
                for i, k in pairs(text) do
                    imgui.TextColored(color[i] or colors[clr.Text], u8(k))
                    imgui.SameLine(nil, 0)
                end
                imgui.NewLine()
            else imgui.Text(u8(w)) end
        end
    end

    render_text(text)
end

function imgui.TextColoredRGB(text)
    local style = imgui.GetStyle()
    local colors = style.Colors
    local ImVec4 = imgui.ImVec4

    local getcolor = function(color)
        if color:sub(1, 6):upper() == "SSSSSS" then
            local r, g, b = colors[1].x, colors[1].y, colors[1].z
            local a = 1.0
            return ImVec4(r, g, b, a / 255)
        end
        local color = type(color) == "string" and tonumber(color, 16) or color
        if type(color) ~= "number" then return end
        local r, g, b, a = explode_argb(color)
        return ImVec4(r/255, g/255, b/255, 1.0)
    end

    local render_text = function(text_)
        for w in text_:gmatch("[^\r\n]+") do
            local text, colors_, m = {}, {}, 1
            w = w:gsub("{(......)}", "{%1FF}")
            while w:find("{........}") do
                local n, k = w:find("{........}")
                local color = getcolor(w:sub(n + 1, k - 1))
                if color then
                    text[#text], text[#text + 1] = w:sub(m, n - 1), w:sub(k + 1, #w)
                    colors_[#colors_ + 1] = color
                    m = n
                end
                w = w:sub(1, n - 1) .. w:sub(k + 1, #w)
            end
            if text[0] then
                for i = 0, #text do
                    imgui.TextColored(colors_[i] or colors[1], u8(text[i]))
                    imgui.SameLine(nil, 0)
                end
                imgui.NewLine()
            else imgui.Text(u8(w)) end
        end
    end

    render_text(text)
end

local array = {
    rosiksettings = imgui.ImBool(false),
    namee = imgui.ImBuffer(50),
    textt1 = imgui.ImBuffer(12),
    textt2 = imgui.ImBuffer(150),
}

    if array.rosiksettings.v then
        local w, h = getScreenResolution()
        imgui.SetNextWindowSize(imgui.ImVec2(903, 350), imgui.Cond.FirstUseEver)
        imgui.SetNextWindowPos(imgui.ImVec2(w / 2, h / 2), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
        imgui.Begin(u8" Настройка розыска", array.rosiksettings, imgui.WindowFlags.NoResize)
        imgui.BeginChild("", imgui.ImVec2(300, 290), true)
        if #mainIniii ~= 0 then
            for i = 1, #mainIniii do
                if imgui.Selectable(u8(mainIniii[i].namee)) then
                    array.namee.v = u8(mainIniii[i].namee)
                    array.textt1.v = string.gsub(u8(mainIniii[i].textt1), "&", "\n")
                    array.textt2.v = string.gsub(u8(mainIniii[i].textt2), "&", "\n")
                    slots = i
                end
            end
        else
            imgui.Text(u8"Тут пока ничего нет")
        end
        imgui.EndChild()
        imgui.SameLine()
        imgui.BeginChild(" ", imgui.ImVec2(579, 315), true)
        if slots ~= 0 then
            imgui.PushItemWidth(350)
            imgui.InputText(u8"Название в диалоге", array.namee)
            imgui.PushItemWidth(20)
            imgui.InputText(u8"Количество звезд", array.textt1)
            imgui.PushItemWidth(350)
            imgui.InputText(u8"Причина при выдачи розыска", array.textt2)
            if imgui.Button(u8" Сохранить", imgui.ImVec2(125, 20)) then
                mainIniii[slots].namee = u8:decode(array.namee.v)
                mainIniii[slots].textt1 = string.gsub(u8:decode(array.textt1.v), "\n", "&")
                mainIniii[slots].textt2 = string.gsub(u8:decode(array.textt2.v), "\n", "&")
                inicfg.save(mainIniii, directIniii)
                sampAddChatMessage('Пункт розыска "' .. u8:decode(array.namee.v) .. '" сохранен.', 0x3399FF)
            end
            --imgui.SameLine()
            if imgui.Button(u8" Удалить", imgui.ImVec2(110, 20)) then
                table.remove(mainIniii,slots)
                inicfg.save(mainIniii,directIniii)
                sampAddChatMessage("Удалено",-1)
                slots = 0
            end
        end
        imgui.EndChild()
        imgui.SetCursorPosY(325)
        if imgui.Button(u8"Добавить строку") then
            local tname = "Пункт розыска № " .. #mainIniii+1
            for k,v in pairs(mainIniii) do
                if mainIniii[k].name == tname then
                    tname = tname .. " (1)"
                end
            end
            table.insert(mainIniii,{
            namee=tname,
            textt1="",
            textt2="",})
            inicfg.save(mainIniii, directIniii)
        end
        imgui.End()
    end
 
Последнее редактирование:

LelHack

Известный
456
123
Регулярку пожалуста
кодик:
Администратор Alexandro_Angelini[964] забанил игрока Abraham_Comisky[445]. Причина: оск род, неадекват || Уэйн
 

W1ll04eison

Участник
328
19
Регулярку пожалуста
кодик:
Администратор Alexandro_Angelini[964] забанил игрока Abraham_Comisky[445]. Причина: оск род, неадекват || Уэйн
Ну уж братан это не бюро заказов, тут помогают, а не делают все за тебя, пишешь код, что то не работает, не понимаешь, кинь сюда - помогут
 

LelHack

Известный
456
123
Ну уж братан это не бюро заказов, тут помогают, а не делают все за тебя, пишешь код, что то не работает, не понимаешь, кинь сюда - помогут
У меня получилось вот так
kod:
Администратор .- забанил игрока .-. Причина: .*
Но увы не work
 

Tol4ek

Активный
217
56
Регулярку пожалуста
кодик:
Администратор Alexandro_Angelini[964] забанил игрока Abraham_Comisky[445]. Причина: оск род, неадекват || Уэйн
Мб и тупая регулярка, но скорее всего так:
Администратор .*%[%d+%] забанил игрока .*%[%d+%]. Причина: .*
 
  • Нравится
Реакции: LelHack

Next..

Известный
345
135
Может я делаю что то не так, но скрипт говорит мне что я сравниваю index с нулем в имгуи кнопке.
Кнопку я поместил в function imgui.OnDrawFrame()
Таблицу с цветами после прогрузки сампа в мейне.
Lua:
local colors = {
    0xAA000000,
    0xAAFF0000,
    0xAA0000FF
}
local index = 1 --все это, до main, в начало скрипта


renderDrawBoxWithBorder(mainIni.pos.x + 40, mainIni.pos.y, 210, 105, colors[index], 1, 0xAA000000) --в бесконечный цикл в main
 
  • Нравится
Реакции: meowkins

PanSeek

t.me/dailypanseek
Всефорумный модератор
899
1,742
Регулярку пожалуста
кодик:
Администратор Alexandro_Angelini[964] забанил игрока Abraham_Comisky[445]. Причина: оск род, неадекват || Уэйн
Lua:
local se = require 'samp.events'
function se.onServerMessage(color, text)
    if text:find('Администратор (%w+_%w+)%[(%d+)%] забанил игрока (%w+_%w+)%[(%d+)%]. Причина: (.+)') then
        local anick, aid, nick, id, other = text:match('Администратор (%w+_%w+)%[(%d+)%] забанил игрока (%w+_%w+)%[(%d+)%]. Причина: (.+)')
        sampAddChatMessage('админ: '..anick..'['..aid..'], игрок: '..nick..'['..id..'], остальное: '..other, -1)
    end
end
Можно получше.
 
  • Нравится
Реакции: LelHack

SurnikSur

Активный
284
40
Код:
local check = {
    "191.1061, -114.6188, 21.0599",
    "190.9305, -129.0596, 20.0058",
}
-- func
local rand = check[math.random(1, #check)]
setCharCoordinates(PLAYER_PED, rand)
помогите я просто тупой
 

chapo

🫡 В армии с 17.10.2023. В ЛС НЕ ОТВЕЧАЮ
Друг
8,747
11,157
Код:
local check = {
    "191.1061, -114.6188, 21.0599",
    "190.9305, -129.0596, 20.0058",
}
-- func
local rand = check[math.random(1, #check)]
setCharCoordinates(PLAYER_PED, rand)
помогите я просто тупой
Lua:
local check = {
    "191.1061, -114.6188, 21.0599",
    "190.9305, -129.0596, 20.0058",
}

math.randomseed(os.clock())
x, y, z = check[math.random(1, #check)]:match('(.+), (.+), (.+)')
setCharCoordinates(PLAYER_PED, tonumber(x), tonumber(y), tonumber(z)
)
или
Lua:
local check = {
    {191.1061, -114.6188, 21.0599},
    {190.9305, -129.0596, 20.0058},
}
math.randomseed(os.clock())
r = math.random(1, #check)
setCharCoordinates(PLAYER_PED, check[r][1], check[r][2], check[r][2])
 
Последнее редактирование:
  • Нравится
Реакции: SurnikSur