вопрос по циклу вывода сообщения через n секунд из ini cfg

TimeLordRu

Известный
Автор темы
151
38
Версия MoonLoader
.026-beta
lua:
local inicfg = require 'inicfg'
local ev = require 'lib.samp.events'


ini = inicfg.load({
Settings =
{
    id = 0
}
})

local status = inicfg.load(ini, 'abilitymech.ini')
if not doesFileExist('moonloader/config/abilitymech.ini') then inicfg.save(ini, 'abilitymech.ini') end

function main()
    while not isSampAvailable() do
    wait(200)
end
sampAddChatMessage("Скрипт для прокачки /ability. Информация: /abil. Автор: {ff0000}TimeLordRu", 0x00ff00)
sampRegisterChatCommand("setid",function(arg)
        ini.Settings.id = tostring(arg)
        inicfg.save(ini, 'abilitymech.ini')
        sampAddChatMessage("id клиента изменен на: "..tostring(arg), -1)
        if active then
        wait (1500)
        sampSendChat("/repair "..tostring(ini.Settings.id).." 1")
        end
    end)
sampRegisterChatCommand("abilmech", abilmech)
sampRegisterChatCommand("abilcar", abilcar)
sampRegisterChatCommand("abil", abil)
wait(-1)
end

function abil()
rat = not rat
if rat then sampAddChatMessage("Если вы водитель авто, то для вас активация /abilauto, если механик, то /abilmech", 0x00ff00) else sampAddChatMessage("Если вы водитель авто, то для вас активация /abilauto, если механик, то /abilmech", 0x00ff00) end
end

function abilmech()
active = not active
if active then sampAddChatMessage("/abilmech было включено.", 0x008000) else sampAddChatMessage("/abilmech было выключено.", 0xFF0000) end
end

function abilcar()
activated = not activated
if activated then sampAddChatMessage("/abilcar было включено.", 0x008000) else sampAddChatMessage("/abilcar было выключено.", 0xFF0000) end
end

function ev.onServerMessage(color, msg)
    if msg:find('произвести ремонт') and activated then sampSendChat("/yes")
        return false
    end
end

Если в функции abilmech () active, то скрипт должен подождать 1,5сек, выслать /repair <id из файла конфиг> 1, и так по циклу, пока это active не станет not active.
Чего бы я ни делал, либо скрипт ничего не делает при ative, либо вообще не запускается. Можете подсказать, что не так?
 
Решение
lua:
local inicfg = require 'inicfg'
local ev = require 'lib.samp.events'


ini = inicfg.load({
Settings =
{
    id = 0
}
})

local status = inicfg.load(ini, 'abilitymech.ini')
if not doesFileExist('moonloader/config/abilitymech.ini') then inicfg.save(ini, 'abilitymech.ini') end

function main()
    while not isSampAvailable() do
    wait(200)
end
sampAddChatMessage("Скрипт для прокачки /ability. Информация: /abil. Автор: {ff0000}TimeLordRu", 0x00ff00)
sampRegisterChatCommand("setid",function(arg)
        ini.Settings.id = tostring(arg)
        inicfg.save(ini, 'abilitymech.ini')
        sampAddChatMessage("id клиента изменен на: "..tostring(arg), -1)
        if active then
        wait (1500)
        sampSendChat("/repair...

PanSeek

t.me/dailypanseek
Всефорумный модератор
899
1,746
lua:
local inicfg = require 'inicfg'
local ev = require 'lib.samp.events'


ini = inicfg.load({
Settings =
{
    id = 0
}
})

local status = inicfg.load(ini, 'abilitymech.ini')
if not doesFileExist('moonloader/config/abilitymech.ini') then inicfg.save(ini, 'abilitymech.ini') end

function main()
    while not isSampAvailable() do
    wait(200)
end
sampAddChatMessage("Скрипт для прокачки /ability. Информация: /abil. Автор: {ff0000}TimeLordRu", 0x00ff00)
sampRegisterChatCommand("setid",function(arg)
        ini.Settings.id = tostring(arg)
        inicfg.save(ini, 'abilitymech.ini')
        sampAddChatMessage("id клиента изменен на: "..tostring(arg), -1)
        if active then
        wait (1500)
        sampSendChat("/repair "..tostring(ini.Settings.id).." 1")
        end
    end)
sampRegisterChatCommand("abilmech", abilmech)
sampRegisterChatCommand("abilcar", abilcar)
sampRegisterChatCommand("abil", abil)
wait(-1)
end

function abil()
rat = not rat
if rat then sampAddChatMessage("Если вы водитель авто, то для вас активация /abilauto, если механик, то /abilmech", 0x00ff00) else sampAddChatMessage("Если вы водитель авто, то для вас активация /abilauto, если механик, то /abilmech", 0x00ff00) end
end

function abilmech()
active = not active
if active then sampAddChatMessage("/abilmech было включено.", 0x008000) else sampAddChatMessage("/abilmech было выключено.", 0xFF0000) end
end

function abilcar()
activated = not activated
if activated then sampAddChatMessage("/abilcar было включено.", 0x008000) else sampAddChatMessage("/abilcar было выключено.", 0xFF0000) end
end

function ev.onServerMessage(color, msg)
    if msg:find('произвести ремонт') and activated then sampSendChat("/yes")
        return false
    end
end

Если в функции abilmech () active, то скрипт должен подождать 1,5сек, выслать /repair <id из файла конфиг> 1, и так по циклу, пока это active не станет not active.
Чего бы я ни делал, либо скрипт ничего не делает при ative, либо вообще не запускается. Можете подсказать, что не так?
Можно сделать так:
Lua:
function abilmech()
    active = not active
    sampAddChatMessage('/abilmech было '..(active and 'включено.' or 'выключено.'), active and 0x008000 or 0xFF0000)
    if active then
        lua_thread.create(function()
            while true do
                wait(1500)
                sampSendChat('/repair '..ini.Settings.id)
            end
        end)
    end
end
Можно сделать по-другому еще
Lua:
function main()
    -- code, delete wait(-1)
    while true do
        wait(1500)
        if active then
            sampSendChat('/repair '..ini.Settings.id)
        end
    end
end

function abilmech()
    active = not active
    sampAddChatMessage('/abilmech было '..(active and 'включено.' or 'выключено.'), active and 0x008000 or 0xFF0000)
end