добавление в массив через фор а после его использование

coldplugg.

Участник
Автор темы
180
22
Версия MoonLoader
.027.0-preview
Lua:
tessaract = {}
for id, player in pairs(getAllPlayers()) do
    if (getDistanceBetweenCoords3d(player.position.x, player.position.y, player.position.z, getBotPosition()) <= 10) then
        table.insert(tessaract, id)
    end
end
sendInput('/plpos '..tessaract[1]..' 1552.9421 -1353.2526 329.4598')
sendInput('/plpos '..tessaract[2]..' 1536.5863 -1353.4635 329.4586')
команду выполняет, но флудит ею как будет в бесконечном цикле и дает ошибку как раз на сендинпут
Код:
attempt to concatenate a nil value
 
Решение
не-а, тоже самое
ну тогда по другому напишу

Попробуй так, как и написал на первом посте, но чуток подправил. Вроде по идее должно сработать
Lua:
-- взято с поста: https://www.blast.hk/threads/19820/
function getDistance(x1, y1, z1, x2, y2, z2) return math.sqrt(((x1-x2)^2) + ((y1-y2)^2) + ((z1-z2)^2)) end

-- ...
local listPlayersInStreamZone = {}
local posX, posY, posZ = getBotPosition()
local listPlayers = getAllPlayers()
for id, player in pairs(listPlayers) do
    if player and player.position and player.position.x then
        local dist = getDistance(player.position.x, player.position.y, player.position.z, posX, posY, posZ)
        if dist <= 10 then
            player.id = id...

kyrtion

Известный
613
224
Lua:
tessaract = {}
for id, player in pairs(getAllPlayers()) do
    if (getDistanceBetweenCoords3d(player.position.x, player.position.y, player.position.z, getBotPosition()) <= 10) then
        table.insert(tessaract, id)
    end
end
sendInput('/plpos '..tessaract[1]..' 1552.9421 -1353.2526 329.4598')
sendInput('/plpos '..tessaract[2]..' 1536.5863 -1353.4635 329.4586')
команду выполняет, но флудит ею как будет в бесконечном цикле и дает ошибку как раз на сендинпут
Код:
attempt to concatenate a nil value

А если так?
RakSAMP Lite | Func: getDistance:
-- взято с поста: https://www.blast.hk/threads/19820/
function getDistance(x1, y1, z1, x2, y2, z2)
    local distance = math.sqrt(((x1-x2)^2) + ((y1-y2)^2) + ((z1-z2)^2))
    return distance
end

-- ...
tessaract = {}
local posX, posY, posZ = getBotPosition()
local listPlayers = getAllPlayers()
for id, player in pairs(listPlayers) do
    if player and player.position and player.position.x then
        local dist = getDistance(player.position.x, player.position.y, player.position.z, posX, posY, posZ)
        if dist <= 10 then
            player.position.distance = dist
            table.insert(tessaract, player)
        end
    end
end
if #tessaract ~= 0 then
    for i, player in pairs(tessaract) do
        print('Ник: '..player.nick..' | Дистанций: '..player.position.distance)
    end
else
    print('Ближе 10 метров никого нет')
end
1685074081442.png


Важно: getDistanceBetweenCoords3d не включено в SAMP.lua и в коробке RakSAMP Lite, а это значит придется создавать собственную функцию.
 
Последнее редактирование:

coldplugg.

Участник
Автор темы
180
22
А если так?
RakSAMP Lite | Func: getDistance:
-- взято с поста: https://www.blast.hk/threads/19820/
function getDistance(x1, y1, z1, x2, y2, z2)
    local distance = math.sqrt(((x1-x2)^2) + ((y1-y2)^2) + ((z1-z2)^2))
    return distance
end

-- ...
tessaract = {}
local posX, posY, posZ = getBotPosition()
local listPlayers = getAllPlayers()
for id, player in pairs(listPlayers) do
    if player and player.position and player.position.x then
        local dist = getDistance(player.position.x, player.position.y, player.position.z, posX, posY, posZ)
        if dist <= 10 then
            player.position.distance = dist
            table.insert(tessaract, player)
        end
    end
end
if #tessaract ~= 0 then
    for i, player in pairs(tessaract) do
        print('Ник: '..player.nick..' | Дистанций: '..player.position.distance)
    end
else
    print('Ближе 10 метров никого нет')
end
Посмотреть вложение 202678

Важно: getDistanceBetweenCoords3d не включено в SAMP.lua и в коробке RakSAMP Lite, а это значит придется создавать собственную функцию.
у меня две точки, мне по игроку на одну нужно
1685085049492.png
как было выше
 

kyrtion

Известный
613
224
у меня две точки, мне по игроку на одну нужно Посмотреть вложение 202692 как было выше
тогда проверяете что существует ключ и позиция, если бы игрок не в стрим-зоне, то по идее таблицы позиции не осуществляются

Lua:
tessaract = {}
for id, player in pairs(getAllPlayers()) do
    if player and player.position and player.position.x and (getDistanceBetweenCoords3d(player.position.x, player.position.y, player.position.z, getBotPosition()) <= 10) then
        table.insert(tessaract, id)
    end
end
sendInput('/plpos '..tessaract[1]..' 1552.9421 -1353.2526 329.4598')
sendInput('/plpos '..tessaract[2]..' 1536.5863 -1353.4635 329.4586')
 

coldplugg.

Участник
Автор темы
180
22
тогда проверяете что существует ключ и позиция, если бы игрок не в стрим-зоне, то по идее таблицы позиции не осуществляются
пример можно? у меня просто в стриме 10 метров хоть 8 игрков будет

тогда проверяете что существует ключ и позиция, если бы игрок не в стрим-зоне, то по идее таблицы позиции не осуществляются

Lua:
tessaract = {}
for id, player in pairs(getAllPlayers()) do
    if player and player.position and player.position.x and (getDistanceBetweenCoords3d(player.position.x, player.position.y, player.position.z, getBotPosition()) <= 10) then
        table.insert(tessaract, id)
    end
end
sendInput('/plpos '..tessaract[1]..' 1552.9421 -1353.2526 329.4598')
sendInput('/plpos '..tessaract[2]..' 1536.5863 -1353.4635 329.4586')
получаю такую же ошибку и флуд
 
Последнее редактирование:

coldplugg.

Участник
Автор темы
180
22
скинь полную ошибку (варнинг типа с консоли)
Код:
[10:40:28] [LUA] C:\Users\coldplugg\Desktop\bot\scripts\game.lua:377: attempt to concatenate a nil value
stack traceback:
    C:\Users\coldplugg\Desktop\bot\scripts\game.lua:377: in function 'callback'
    ...\coldplugg\Desktop\bot\scripts\libs\samp\events\core.lua:74: in function <...\coldplugg\Desktop\bot\scripts\libs\samp\events\core.lua:48>
377 строка отправка
Lua:
sendInput('/plpos '..tessaract[1]..' 1552.9421 -1353.2526 329.4598')
 

kyrtion

Известный
613
224
Попробуй так
Lua:
function getDistance(x1, y1, z1, x2, y2, z2)
    return math.sqrt(((x1-x2)^2) + ((y1-y2)^2) + ((z1-z2)^2))
end

-- ...
local tessaract = {}
local listPlayers = getAllPlayers()
local posX, posY, posZ = getBotPosition()
for id, player in pairs(listPlayers) do
    if player and player.position and player.position.x then
        local dist = getDistance(player.position.x, player.position.y, player.position.z, posX, posY, posZ)
        if dist <= 10 then
            table.insert(tessaract, id)
        end
    end
end
sendInput('/plpos '..tessaract[1]..' 1552.9421 -1353.2526 329.4598')
sendInput('/plpos '..tessaract[2]..' 1536.5863 -1353.4635 329.4586')
 

coldplugg.

Участник
Автор темы
180
22
Попробуй так
Lua:
function getDistance(x1, y1, z1, x2, y2, z2)
    return math.sqrt(((x1-x2)^2) + ((y1-y2)^2) + ((z1-z2)^2))
end

-- ...
local tessaract = {}
local listPlayers = getAllPlayers()
local posX, posY, posZ = getBotPosition()
for id, player in pairs(listPlayers) do
    if player and player.position and player.position.x then
        local dist = getDistance(player.position.x, player.position.y, player.position.z, posX, posY, posZ)
        if dist <= 10 then
            table.insert(tessaract, id)
        end
    end
end
sendInput('/plpos '..tessaract[1]..' 1552.9421 -1353.2526 329.4598')
sendInput('/plpos '..tessaract[2]..' 1536.5863 -1353.4635 329.4586')
не-а, тоже самое
 

kyrtion

Известный
613
224
не-а, тоже самое
ну тогда по другому напишу

Попробуй так, как и написал на первом посте, но чуток подправил. Вроде по идее должно сработать
Lua:
-- взято с поста: https://www.blast.hk/threads/19820/
function getDistance(x1, y1, z1, x2, y2, z2) return math.sqrt(((x1-x2)^2) + ((y1-y2)^2) + ((z1-z2)^2)) end

-- ...
local listPlayersInStreamZone = {}
local posX, posY, posZ = getBotPosition()
local listPlayers = getAllPlayers()
for id, player in pairs(listPlayers) do
    if player and player.position and player.position.x then
        local dist = getDistance(player.position.x, player.position.y, player.position.z, posX, posY, posZ)
        if dist <= 10 then
            player.id = id
            player.position.distance = dist
            table.insert(listPlayersInStreamZone, player)
        end
    end
end
local count = 0
if #listPlayersInStreamZone ~= 0 then
    for i, player in pairs(listPlayersInStreamZone) do
        print('Ник: '..player.nick..' | Дистанций: '..player.position.distance)
        if count == 0 then
            sendInput('/plpos '..player.id..' 1552.9421 -1353.2526 329.4598')
            count = count + 1
        elseif count == 1 then
            sendInput('/plpos '..player.id..' 1536.5863 -1353.4635 329.4586')
            count = count + 1
        end
    end
else
    print('Ближе 10 метров никого нет')
end

Если в зоне стрима 10 метров более 3 игроков, и все равно их телепортировать на точке, то:
Lua:
-- взято с поста: https://www.blast.hk/threads/19820/
function getDistance(x1, y1, z1, x2, y2, z2) return math.sqrt(((x1-x2)^2) + ((y1-y2)^2) + ((z1-z2)^2)) end

-- ...
local listPlayersInStreamZone = {}
local posX, posY, posZ = getBotPosition()
local listPlayers = getAllPlayers()
for id, player in pairs(listPlayers) do
    if player and player.position and player.position.x then
        local dist = getDistance(player.position.x, player.position.y, player.position.z, posX, posY, posZ)
        if dist <= 10 then
            player.id = id
            player.position.distance = dist
            table.insert(listPlayersInStreamZone, player)
        end
    end
end
local boolSelectedPlayerInStreamZone = false
if #listPlayersInStreamZone ~= 0 then
    for i, player in pairs(listPlayersInStreamZone) do
        print('Ник: '..player.nick..' | Дистанций: '..player.position.distance)
        boolSelectedPlayerInStreamZone = not boolSelectedPlayerInStreamZone
        if boolSelectedPlayerInStreamZone then
            sendInput('/plpos '..player.id..' 1552.9421 -1353.2526 329.4598') -- 1 точка
        else
            sendInput('/plpos '..player.id..' 1536.5863 -1353.4635 329.4586') -- 2 точка
        end
    end
else
    print('Ближе 10 метров никого нет')
end
 
Последнее редактирование:

coldplugg.

Участник
Автор темы
180
22
ну тогда по другому напишу

Попробуй так, как и написал на первом посте, но чуток подправил. Вроде по идее должно сработать
Lua:
-- взято с поста: https://www.blast.hk/threads/19820/
function getDistance(x1, y1, z1, x2, y2, z2) return math.sqrt(((x1-x2)^2) + ((y1-y2)^2) + ((z1-z2)^2)) end

-- ...
local listPlayersInStreamZone = {}
local posX, posY, posZ = getBotPosition()
local listPlayers = getAllPlayers()
for id, player in pairs(listPlayers) do
    if player and player.position and player.position.x then
        local dist = getDistance(player.position.x, player.position.y, player.position.z, posX, posY, posZ)
        if dist <= 10 then
            player.id = id
            player.position.distance = dist
            table.insert(listPlayersInStreamZone, player)
        end
    end
end
local count = 0
if #listPlayersInStreamZone ~= 0 then
    for i, player in pairs(listPlayersInStreamZone) do
        print('Ник: '..player.nick..' | Дистанций: '..player.position.distance)
        if count == 0 then
            sendInput('/plpos '..player.id..' 1552.9421 -1353.2526 329.4598')
            count = count + 1
        elseif count == 1 then
            sendInput('/plpos '..player.id..' 1536.5863 -1353.4635 329.4586')
            count = count + 1
        end
    end
else
    print('Ближе 10 метров никого нет')
end

Если в зоне стрима 10 метров более 3 игроков, и все равно их телепортировать на точке, то:
Lua:
-- взято с поста: https://www.blast.hk/threads/19820/
function getDistance(x1, y1, z1, x2, y2, z2) return math.sqrt(((x1-x2)^2) + ((y1-y2)^2) + ((z1-z2)^2)) end

-- ...
local listPlayersInStreamZone = {}
local posX, posY, posZ = getBotPosition()
local listPlayers = getAllPlayers()
for id, player in pairs(listPlayers) do
    if player and player.position and player.position.x then
        local dist = getDistance(player.position.x, player.position.y, player.position.z, posX, posY, posZ)
        if dist <= 10 then
            player.id = id
            player.position.distance = dist
            table.insert(listPlayersInStreamZone, player)
        end
    end
end
local boolSelectedPlayerInStreamZone = false
if #listPlayersInStreamZone ~= 0 then
    for i, player in pairs(listPlayersInStreamZone) do
        print('Ник: '..player.nick..' | Дистанций: '..player.position.distance)
        boolSelectedPlayerInStreamZone = not boolSelectedPlayerInStreamZone
        if boolSelectedPlayerInStreamZone then
            sendInput('/plpos '..player.id..' 1552.9421 -1353.2526 329.4598') -- 1 точка
        else
            sendInput('/plpos '..player.id..' 1536.5863 -1353.4635 329.4586') -- 2 точка
        end
    end
else
    print('Ближе 10 метров никого нет')
end
раз 15 дает ник | дистанция и плпос, потом как будто while true начинает флудить ближе 10 метров никого нет
 

kyrtion

Известный
613
224
раз 15 дает ник | дистанция и плпос, потом как будто while true начинает флудить ближе 10 метров никого нет
их можете убрать (закомментировать на print, а также в конце else и print) , но лучше протестируйте на присутствие ракбота с игроками
p.s. я написал для команды в onRunCommand
 
Последнее редактирование:

coldplugg.

Участник
Автор темы
180
22
их можете убрать (закомментировать на print, а также в конце else и print) , но лучше протестируйте на присутствие ракбота с игроками
p.s. я написал для команды в onRunCommand
меня смущает что он командой флудит
1685089809518.png