Таблицы массивы

Fasmin

Участник
Автор темы
175
6
Версия MoonLoader
.026-beta
Как в таблицу или массив засунуть несколько аргументов? Типо мне нужно допустим занести туда несколько айди игроков, которые находятся в 5 метрах от меня и потом вывести в sampAddChatMessage

Вроде как должно получится так
123
455
656
566
576
 
Решение
Как в таблицу или массив засунуть несколько аргументов? Типо мне нужно допустим занести туда несколько айди игроков, которые находятся в 5 метрах от меня и потом вывести в sampAddChatMessage

Вроде как должно получится так
123
455
656
566
576
Lua:
local arroy = {}
function main()
    while not isSampAvailable() do wait(0) end
        sampRegisterChatCommand('getchars', function()
            for i, v in ipairs(getAllChars()) do
                local x, y, z = getCharCoordinates(1)
                local px, py, pz = getCharCoordinates(v)
                local dist = getDistanceBetweenCoords3d(x,y,z,px,py,pz)
                if dist >= 5 then
                    table.insert(arroy, select(2, sampGetPlayerIdByCharHandle(v)))...

ChаtGPT

Активный
404
97
через table.insert(*таблица, в которую заносишь*, *что заносишь*)
это в цикле
потом циклом парсишь эту таблицу и выводишь

 

VanoKLR

Известный
965
591
Как в таблицу или массив засунуть несколько аргументов? Типо мне нужно допустим занести туда несколько айди игроков, которые находятся в 5 метрах от меня и потом вывести в sampAddChatMessage

Вроде как должно получится так
123
455
656
566
576
Lua:
local arroy = {}
function main()
    while not isSampAvailable() do wait(0) end
        sampRegisterChatCommand('getchars', function()
            for i, v in ipairs(getAllChars()) do
                local x, y, z = getCharCoordinates(1)
                local px, py, pz = getCharCoordinates(v)
                local dist = getDistanceBetweenCoords3d(x,y,z,px,py,pz)
                if dist >= 5 then
                    table.insert(arroy, select(2, sampGetPlayerIdByCharHandle(v)))
                end
            end
        end)
        sampRegisterChatCommand('showchars', function()
            print("Всего в списке: ", #arroy)
            for i, v in ipairs(arroy) do
                print(v)
            end
        end)
    wait(-1)
end
 

Fasmin

Участник
Автор темы
175
6
Lua:
local arroy = {}
function main()
    while not isSampAvailable() do wait(0) end
        sampRegisterChatCommand('getchars', function()
            for i, v in ipairs(getAllChars()) do
                local x, y, z = getCharCoordinates(1)
                local px, py, pz = getCharCoordinates(v)
                local dist = getDistanceBetweenCoords3d(x,y,z,px,py,pz)
                if dist >= 5 then
                    table.insert(arroy, select(2, sampGetPlayerIdByCharHandle(v)))
                end
            end
        end)
        sampRegisterChatCommand('showchars', function()
            print("Всего в списке: ", #arroy)
            for i, v in ipairs(arroy) do
                print(v)
            end
        end)
    wait(-1)
end
Выводит почему то так:
1
2
3
код:
local hallall = {}

sampRegisterChatCommand("hl", function()
    lua_thread.create(function()
        for i=0, sampGetMaxPlayerId() do
            if sampIsPlayerConnected(i) then
                local x,y,z = getCharCoordinates(ped)
                local __, char = sampGetCharHandleBySampPlayerId(i)
                if __ then
                    local xx,yy,zz = getCharCoordinates(char)
                    if getDistanceBetweenCoords3d(x,y,z, xx,yy,zz) <= 4 then
                        sampSendChat("/heal " .. i)
                        sampAddChatMessage(i, -1)
                        table.insert(hallall, i)
                    end
                end
            end
        end
    end)
end)



sampRegisterChatCommand("test", function()
    for i in ipairs(hallall) do
        sampAddChatMessage(i, -1)
    end
end)

Вот такой код, рабочий, а прикол в том, что в сампаддчатмеседже айдишник норм выводит, а из таблицы хуй
 
Последнее редактирование:

chapo

tg/inst: @moujeek
Модератор
9,078
12,064
Выводит почему то так:
1
2
3
код:
local hallall = {}

sampRegisterChatCommand("hl", function()
    lua_thread.create(function()
        for i=0, sampGetMaxPlayerId() do
            if sampIsPlayerConnected(i) then
                local x,y,z = getCharCoordinates(ped)
                local __, char = sampGetCharHandleBySampPlayerId(i)
                if __ then
                    local xx,yy,zz = getCharCoordinates(char)
                    if getDistanceBetweenCoords3d(x,y,z, xx,yy,zz) <= 4 then
                        sampSendChat("/heal " .. i)
                        sampAddChatMessage(i, -1)
                        table.insert(hallall, i)
                    end
                end
            end
        end
    end)
end)



sampRegisterChatCommand("test", function()
    for i in ipairs(hallall) do
        sampAddChatMessage(i, -1)
    end
end)

Вот такой код, рабочий, а прикол в том, что в сампаддчатмеседже айдишник норм выводит, а из таблицы хуй
ну так потому что ты выводишь не значения из таблицы, а индексы значений. замени
Lua:
for i in ipairs(hallall) do
    sampAddChatMessage(i, -1)
end
на
Lua:
for i, id in ipairs(hallall) do
    sampAddChatMessage(id, -1)
end
 
  • Нравится
Реакции: Fasmin