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

D.Makarov

Участник
146
3
Как сделать тип, при наводке на игрока открывалось меню, и там я добавлю тип всякие кнопки и тд.. По типу
Посмотреть вложение 90534

Lua:
script_name('FamilyHelper v 1.0')
script_author('Tsunami_Nakamura')
script_description('FamilyHelper v 1.0')
script_version('1.0')

require "lib.moonloader"
local imgui = require 'imgui'
local key = require 'vkeys'

local act = 0

function apply_custom_style()
local style = imgui.GetStyle()
local colors = style.Colors
local clr = imgui.Col
local ImVec4 = imgui.ImVec4

colors[clr.Button] = ImVec4(0.20, 0.25, 0.28, 1.00)
colors[clr.ButtonHovered] = ImVec4(0.20, 0.25, 0.28, 1.00)
colors[clr.ButtonActive] = ImVec4(0.20, 0.25, 0.28, 1.00)
end
apply_custom_style()

local main_window_state = imgui.ImBool(false)
local checkbox = imgui.ImBool(false)

function imgui.OnDrawFrame()
local iScreenWidth, iScreenHeight = getScreenResolution()
local btn_size = imgui.ImVec2(-1, 0)
if main_window_state.v then
imgui.SetNextWindowSize(imgui.ImVec2(1000, 500), imgui.Cond.FirstUseEver)
imgui.Begin('Family Helper by Nakamura', main_window_state, imgui.WindowFlags.NoResize + imgui.WindowFlags.NoCollapse)
imgui.BeginChild('##main menu', imgui.ImVec2(120, 460), true)
if imgui.Button('Основное\n меню', imgui.ImVec2(-1, 50)) then act = 0 end
if imgui.Button('Управление\nУчастниками', imgui.ImVec2(-1, 50)) then act = 1 end
if imgui.Button('Система\nПовышений', imgui.ImVec2(-1, 50)) then act = 2 end
if imgui.Button('Биндер\nдля пиара', imgui.ImVec2(-1, 50)) then act = 3 end
if imgui.Button('Проверка на\nVIP статус', imgui.ImVec2(-1, 50)) then act = 4 end
if imgui.Button('Дискорд\nсемьи', imgui.ImVec2(-1, 50)) then act = 5 end
if imgui.Button('Правила\ndiscord', imgui.ImVec2(-1, 50)) then act = 6 end
if imgui.Button('Правила\nсемьи', imgui.ImVec2(-1, 50)) then act = 7 end
if imgui.Button('Выдача\nНаказаний', imgui.ImVec2(-1, 50)) then act = 8 end
if imgui.Button('Система\nРАНГОВ', imgui.ImVec2(-1, 50)) then act = 9 end
imgui.EndChild()
imgui.SameLine()
if act == 0 then
imgui.BeginChild('##once', imgui.ImVec2(730, 460), true)
imgui.Text('Автор скрипта: Tsunami_Nakamura. Помощник: Adam_Karleone')
imgui.Text('Данный хелпер находится в разработке')
imgui.Text('Есть вопросы, или нашли не доработку?')
imgui.Text('Напишите мне в ВК - @lkn.maks.')
imgui.EndChild()
elseif act == 1 then
imgui.BeginChild('##twice', imgui.ImVec2(730, 460), true)
imgui.Button('Онлайн игроки', imgui.ImVec2(800,20))
imgui.Button('Оффлайн игроки', imgui.ImVec2(800,20))
imgui.EndChild()
end
if act == 2 then
imgui.BeginChild('##twice', imgui.ImVec2(730, 460), true)
imgui.Button('Повышение с 1 на 2 РАНГ', imgui.ImVec2(800,20)) then act = 21 end
imgui.Button('Повышение с 2 на 3 РАНГ', imgui.ImVec2(800,20)) then act = 22 end
imgui.Button('Повышение с 3 на 4 РАНГ', imgui.ImVec2(800,20)) then act = 23 end
imgui.Button('Повышение с 4 на 5 РАНГ', imgui.ImVec2(800,20)) then act = 24 end
imgui.Button('Повышение с 5 на 6 РАНГ', imgui.ImVec2(800,20)) then act = 25 end
imgui.Button('Повышение с 6 на 7 РАНГ', imgui.ImVec2(800,20)) then act = 26 end
imgui.Button('Повышение с 7 на 8 РАНГ', imgui.ImVec2(800,20)) then act = 27 end
imgui.Button('Повышение с 8 на 9 РАНГ', imgui.ImVec2(800,20)) then act = 28 end
imgui.Button('Повышение с 9 на 10 РАНГ', imgui.ImVec2(800,20)) then act = 29 end
imgui.EndChild()
elseif act == 21 then
imgui.BeginChild('#1', imgui.ImVec2(730, 460), true)
if imgui.Button('EXIT', imgui.ImVec2(40,20)) then act = 2 end
imgui.SameLine()
imgui.Text(u8'1212')
imgui.Text(u8'1212')
imgui.EndChild()
elseif act == 22 then
imgui.BeginChild('#2', imgui.ImVec2(730, 460), true)
if imgui.Button('EXIT', imgui.ImVec2(40,20)) then act = 2 end
imgui.SameLine()
imgui.Text(u8'1212')
imgui.Text(u8'1212')
imgui.EndChild()
elseif act == 23 then
imgui.BeginChild('#3', imgui.ImVec2(730, 460), true)
if imgui.Button('EXIT', imgui.ImVec2(40,20)) then act = 2 end
imgui.SameLine()
imgui.Text(u8'1212')
imgui.Text(u8'1212')
imgui.EndChild()
elseif act == 24 then
imgui.BeginChild('#4', imgui.ImVec2(730, 460), true)
if imgui.Button('EXIT', imgui.ImVec2(40,20)) then act = 2 end
imgui.SameLine()
imgui.Text(u8'1212')
imgui.Text(u8'1212')
imgui.EndChild()
elseif act == 25 then
imgui.BeginChild('#5', imgui.ImVec2(730, 460), true)
if imgui.Button('EXIT', imgui.ImVec2(40,20)) then act = 2 end
imgui.SameLine()
imgui.Text(u8'1212')
imgui.Text(u8'1212')
imgui.EndChild()
elseif act == 26 then
imgui.BeginChild('#6', imgui.ImVec2(730, 460), true)
if imgui.Button('EXIT', imgui.ImVec2(40,20)) then act = 2 end
imgui.SameLine()
imgui.Text(u8'1212')
imgui.Text(u8'1212')
imgui.EndChild()
elseif act == 27 then
imgui.BeginChild('#7', imgui.ImVec2(730, 460), true)
if imgui.Button('EXIT', imgui.ImVec2(40,20)) then act = 2 end
imgui.SameLine()
imgui.Text(u8'1212')
imgui.Text(u8'1212')
imgui.EndChild()
elseif act == 28 then
imgui.BeginChild('#8', imgui.ImVec2(730, 460), true)
if imgui.Button('EXIT', imgui.ImVec2(40,20)) then act = 2 end
imgui.SameLine()
imgui.Text(u8'1212')
imgui.Text(u8'1212')
imgui.EndChild()
elseif act == 29 then
imgui.BeginChild('#9', imgui.ImVec2(730, 460), true)
if imgui.Button('EXIT', imgui.ImVec2(40,20)) then act = 2 end
imgui.SameLine()
imgui.Text(u8'1212')
imgui.Text(u8'1212')
imgui.EndChild()
end
imgui.SameLine()
imgui.BeginChild('##tabs2', imgui.ImVec2(120, 460), true)
if imgui.Button('Квесты', imgui.ImVec2(-1, 50)) then act = 10 end
if imgui.Button('Выговоры(R)', imgui.ImVec2(-1, 50)) then act = 11 end
if imgui.Button('Конкурсы\n и\nРозыграши', imgui.ImVec2(-1, 50)) then act = 12 end
if imgui.Button('Информация\n о семье', imgui.ImVec2(-1, 50)) then act = 13 end
if imgui.Button('РП отыгровки', imgui.ImVec2(-1, 50)) then act = 14 end
if imgui.Button('Настройки\n игры', imgui.ImVec2(-1, 50)) then act = 15 end
if imgui.Button('Система\n крафта', imgui.ImVec2(-1, 50)) then act = 16 end
if imgui.Button('', imgui.ImVec2(-1, 50)) then act = 17 end
if imgui.Button('CMD list', imgui.ImVec2(-1, 50)) then act = 18 end
if imgui.Button('Family', imgui.ImVec2(-1, 50)) then act = 19 end
imgui.EndChild()
imgui.End()
end
end


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

sampAddChatMessage('[FamHelper] Автор скрипта: Tsunami_Nakamura. Помощник: Adam_Karleone [ARIZONA 10]', 0x00BFFF)
sampAddChatMessage('[FamHelper] Есть вопросы? ВК - lcn.maks', 0x00BFFF)
sampAddChatMessage('[FamHelper] Активация хелпера: /famh', 0x00BFFF)
sampAddChatMessage('[FamHelper] Нашли баг или не доработку? Напишите в ВК', 0x00BFFF)
sampRegisterChatCommand('famh', function() main_window_state.v = not main_window_state.v end)

while true do
wait(0)
imgui.Process = main_window_state.v and true or false

if checkbox.v then
printStringNow('test', 1000)
end
end
end
ну тип тригер на хендл сделать
Lua:
local valid, ped = getCharPlayerIsTargeting(PLAYER_HANDLE) -- получить хендл персонажа, в которого целится игрок
if valid and doesCharExist(ped) then -- если цель есть и персонаж существует
  local result, id = sampGetPlayerIdByCharHandle(ped) -- получить samp-ид игрока по хендлу персонажа
  if result then -- проверить, прошло ли получение ида успешно
    -- здесь любые действия с полученным идом игрока
  end
end
 

sep

Известный
681
77
Lua:
function BackDistance()
    for i = 0, 2048 do
        if text3d[i] ~= nil then
            local text, color, posX, posY, posZ, distance, ignoreWalls, playerId, vehicleId = sampGet3dTextInfoById(i)
            sampCreate3dTextEx(i, text, color, posX, posY, posZ, text3d[i], ignoreWalls, playerId, vehicleId)
        end
    end
end

function hook.onCreate3DText(id, color, pos, distance, testLOS, attachedPlayerId, attachedVehicleId, text)
    text3d[id] = distance
    sampCreate3dTextEx(id, text, color, pos.x, pos.y, pos.z, 1000.0, testLOS, attachedPlayerId, attachedVehicleId)
    return false
end

function hook.onRemove3DTextLabel(id)
    text3d[id] = nil
    sampDestroy3dText(id)
    return false
end

Дальше сам
непашет (
 

Narkizo

Известный
37
14
Хочу сделать задержку в этом отрывке кода, но если вставляю между командами wait(15), вторую команду не прописывает. Как сделать так, чтобы прописывало сначала одну и потом через время другую?
lua:
while not isSampAvailable() do wait(0) end
sampRegisterChatCommand('vd', function(arg)
if arg:find('%d+') then
sampSendChat('/slap '..tostring(arg)..' 1')
sampSendChat('/gethere '..tostring(arg)..'')
else
sampAddChatMessage('Используйте /vd [ID]',-1)
end
end)
 

Fott

Простреленный
3,469
2,390
Хочу сделать задержку в этом коде, но если вставляю между ними wait(15), вторую команду не прописывает. Как сделать так, чтобы прописывало сначала одну и потом через время другую?
lua:
while not isSampAvailable() do wait(0) end
sampRegisterChatCommand('vd', function(arg)
if arg:find('%d+') then
sampSendChat('/slap '..tostring(arg)..' 1')
sampSendChat('/gethere '..tostring(arg)..'')
else
sampAddChatMessage('Используйте /vd [ID]',-1)
end
end)
Для задержки все мейна нужен поток
 

D.Makarov

Участник
146
3
Ребята у меня есть функция
Lua:
function sampev.onSendPlayerSync(data)
    if activ then
    data.position.x = 2012
    data.position.y = -1989
    data.position.z = 13
end
end
как сделать что бы она активировалась когда в чать будет определённое сообщение
 

PanSeek

t.me/dailypanseek
Всефорумный модератор
910
1,779
Ребята у меня есть функция
Lua:
function sampev.onSendPlayerSync(data)
    if activ then
    data.position.x = 2012
    data.position.y = -1989
    data.position.z = 13
end
end
как сделать что бы она активировалась когда в чать будет определённое сообщение

Lua:
function sampev.onServerMessage(color, text)
    if text:find("Hello world!") then
        activ = true
    end
end
 
  • Нравится
Реакции: James Saula

D.Makarov

Участник
146
3
Lua:
function sampev.onSendPlayerSync(data)
    if activ then
    lua_thread.create(function()
    wait(3000)
    data.position.x = 2015
    data.position.y = -1958
    data.position.z = 14
    sampSendClickTextdraw(785)
       sampSendClickTextdraw(784)
       wait(5000)
       data.position.x = 2014
    data.position.y = -1971
    data.position.z = 14
    wait(3000)
    data.position.x = 2012
    data.position.y = -1989
    data.position.z = 13
    end)
    end
    end
мб я тупой или баг, но почему не ставиться задержка я не могу понять
 

Smeruxa

smeruxa.ru
Проверенный
1,412
757
Lua:
function sampev.onSendPlayerSync(data)
    if activ then
    lua_thread.create(function()
    wait(3000)
    data.position.x = 2015
    data.position.y = -1958
    data.position.z = 14
    sampSendClickTextdraw(785)
       sampSendClickTextdraw(784)
       wait(5000)
       data.position.x = 2014
    data.position.y = -1971
    data.position.z = 14
    wait(3000)
    data.position.x = 2012
    data.position.y = -1989
    data.position.z = 13
    end)
    end
    end
мб я тупой или баг, но почему не ставиться задержка я не могу понять
Исправить табуляцию и помогу..
 

PanSeek

t.me/dailypanseek
Всефорумный модератор
910
1,779
Исправить табуляцию и помогу..
Так делается в пару тыков
Lua:
function sampev.onSendPlayerSync(data)
    if activ then
        lua_thread.create(function()
            wait(3000)
            data.position.x = 2015
            data.position.y = -1958
            data.position.z = 14
            sampSendClickTextdraw(785)
            sampSendClickTextdraw(784)
            wait(5000)
            data.position.x = 2014
            data.position.y = -1971
            data.position.z = 14
            wait(3000)
            data.position.x = 2012
            data.position.y = -1989
            data.position.z = 13
        end)
    end
end
А ты глянь, тебе понятно его так читать и не путаться? Вот мне неприятно лично и многим другим.
 
  • Нравится
Реакции: James Saula

D.Makarov

Участник
146
3
Так делается в пару тыков
Lua:
function sampev.onSendPlayerSync(data)
    if activ then
        lua_thread.create(function()
            wait(3000)
            data.position.x = 2015
            data.position.y = -1958
            data.position.z = 14
            sampSendClickTextdraw(785)
            sampSendClickTextdraw(784)
            wait(5000)
            data.position.x = 2014
            data.position.y = -1971
            data.position.z = 14
            wait(3000)
            data.position.x = 2012
            data.position.y = -1989
            data.position.z = 13
        end)
    end
end

А ты глянь, тебе понятно его так читать и не путаться? Вот мне неприятно лично и многим другим.
всё равно не работает флудит всё что можно а не по очереди
 

Myroslaw

Известный
133
5
Lua:
local event = require 'lib.samp.events'

sampAddChatMessage('Stream hack {008000}loaded. {FFFFFF}Command: {FF0000}/stream', -1)

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

    sampRegisterChatCommand('stream', cmd_stream)

    while true do
        wait(0)
    
    end
end

function cmd_stream()
    enabled = not enabled

    sampAddChatMessage(enabled and 'Інформація про гравців в промальовуванні {008000}включена' or 'Інформація про гравців в промальовуванні {FF0000}виключена', -1)
end

function event.onPlayerStreamIn(playerId)
    if enabled then
        sampAddChatMessage('В зоні промальовування появився гравець: {FF0000}'..sampGetPlayerNickname(playerId)..' ('..playerId..')', -1)
    end
end

function event.onPlayerStreamOut(playerId)
    if enabled then
        sampAddChatMessage('Зони промальовування покинув гравець: {008000}'..sampGetPlayerNickname(playerId)..' ('..playerId..')', -1)
    end
end
Можно как то оптимизировать?
 

deloren96

Известный
11
13
Lua:
local event = require 'lib.samp.events'

sampAddChatMessage('Stream hack {008000}loaded. {FFFFFF}Command: {FF0000}/stream', -1)

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

    sampRegisterChatCommand('stream', cmd_stream)

    while true do
        wait(0)
   
    end
end

function cmd_stream()
    enabled = not enabled

    sampAddChatMessage(enabled and 'Інформація про гравців в промальовуванні {008000}включена' or 'Інформація про гравців в промальовуванні {FF0000}виключена', -1)
end

function event.onPlayerStreamIn(playerId)
    if enabled then
        sampAddChatMessage('В зоні промальовування появився гравець: {FF0000}'..sampGetPlayerNickname(playerId)..' ('..playerId..')', -1)
    end
end

function event.onPlayerStreamOut(playerId)
    if enabled then
        sampAddChatMessage('Зони промальовування покинув гравець: {008000}'..sampGetPlayerNickname(playerId)..' ('..playerId..')', -1)
    end
end
Можно как то оптимизировать?
Так тут ничего и нет толком, чтобы что-то оптимизировать. Можешь попробовать салом смазать