хэндл игрока в зоне стрима

$Mr.R1ch$

Активный
Автор темы
330
51
Версия MoonLoader
Другое
Как правильно получить ID ближайшего игрока в зоне стрима? пытался сделать сам, и ничего не выводит. (не помню как там дистанцию прописывать)
 
Решение
Lua:
function main()
    if not isSampfuncsLoaded() or not isSampLoaded() then return end
    sampRegisterChatCommand("hplayer", player_handle_get)

    while true do wait(0) end
end


function getNearestID()
    local chars = getAllChars()
    local mx, my, mz = getCharCoordinates(PLAYER_PED)
    local nearId, dist = nil, 10000
    for i,v in ipairs(chars) do
        if doesCharExist(v) and v ~= PLAYER_PED then
            local vx, vy, vz = getCharCoordinates(v)
            local cDist = getDistanceBetweenCoords3d(mx, my, mz, vx, vy, vz)
            local r, id = sampGetPlayerIdByCharHandle(v)
            if r and cDist < dist then
                dist = cDist
                nearId = id
            end
        end
    end
    return...

MLycoris

На вид оружие массового семяизвержения
Проверенный
1,988
2,190
не понял хендл или всё таки ид? Вот вариант с идом
Lua:
function nearestPlayerId()
    local minDist = 500
    local savedId = -1
    local mx, my, mz = getCharCoordinates(1)
    for k, handle in pairs(getAllChars()) do
        local cx, cy, cz = getCharCoordinates(handle)
        local dist = getDistanceBetweenCoords3d(mx, my, mz, cx, cy, cz)
        local res, id = sampGetPlayerIdByCharHandle(handle)
        if dist < minDist and res and handle ~= 1 then
            minDist = dist
            savedId = id
        end
    end
    return savedId
end
 
  • Нравится
Реакции: $Mr.R1ch$

IlyaFontonov

Активный
177
41
Lua:
function main()
    if not isSampfuncsLoaded() or not isSampLoaded() then return end
    sampRegisterChatCommand("hplayer", player_handle_get)

    while true do wait(0) end
end


function getNearestID()
    local chars = getAllChars()
    local mx, my, mz = getCharCoordinates(PLAYER_PED)
    local nearId, dist = nil, 10000
    for i,v in ipairs(chars) do
        if doesCharExist(v) and v ~= PLAYER_PED then
            local vx, vy, vz = getCharCoordinates(v)
            local cDist = getDistanceBetweenCoords3d(mx, my, mz, vx, vy, vz)
            local r, id = sampGetPlayerIdByCharHandle(v)
            if r and cDist < dist then
                dist = cDist
                nearId = id
            end
        end
    end
    return nearId
end

function player_handle_get()
    local id = getNearestID()
    if id then
        result, handle = sampGetCharHandleBySampPlayerId(id)
        sampAddChatMessage('nearest id '..id, -1)
    else
        -- Ближайший игрок не найден
    end
end

Lua:
function main()
    if not isSampfuncsLoaded() or not isSampLoaded() then return end
    sampRegisterChatCommand("hplayer", player_handle_get)

    while true do wait(0) end
end


function getNearestID()
    local chars = getAllChars()
    local mx, my, mz = getCharCoordinates(PLAYER_PED)
    local nearId, dist = nil, 10000
    for i,v in ipairs(chars) do
        if doesCharExist(v) and v ~= PLAYER_PED then
            local vx, vy, vz = getCharCoordinates(v)
            local cDist = getDistanceBetweenCoords3d(mx, my, mz, vx, vy, vz)
            local r, id = sampGetPlayerIdByCharHandle(v)
            if r and cDist < dist then
                dist = cDist
                nearId = id
            end
        end
    end
    return nearId
end

function player_handle_get()
    local id = getNearestID()
    if id then
        result, handle = sampGetCharHandleBySampPlayerId(id)
        sampAddChatMessage('nearest id '..id, -1)
    else
        -- Ближайший игрок не найден
    end
end
handle+id
 
  • Влюблен
Реакции: $Mr.R1ch$

$Mr.R1ch$

Активный
Автор темы
330
51
не понял хендл или всё таки ид? Вот вариант с идом
Lua:
function nearestPlayerId()
    local minDist = 500
    local savedId = -1
    local mx, my, mz = getCharCoordinates(1)
    for k, handle in pairs(getAllChars()) do
        local cx, cy, cz = getCharCoordinates(handle)
        local dist = getDistanceBetweenCoords3d(mx, my, mz, cx, cy, cz)
        local res, id = sampGetPlayerIdByCharHandle(handle)
        if dist < minDist and res and handle ~= 1 then
            minDist = dist
            savedId = id
        end
    end
    return savedId
end
если в зоне стрима стоит 2+ игрока, то выводит айди обоих, несмотря что второй игрок дальше первого

Lua:
function main()
    if not isSampfuncsLoaded() or not isSampLoaded() then return end
    sampRegisterChatCommand("hplayer", player_handle_get)

    while true do wait(0) end
end


function getNearestID()
    local chars = getAllChars()
    local mx, my, mz = getCharCoordinates(PLAYER_PED)
    local nearId, dist = nil, 10000
    for i,v in ipairs(chars) do
        if doesCharExist(v) and v ~= PLAYER_PED then
            local vx, vy, vz = getCharCoordinates(v)
            local cDist = getDistanceBetweenCoords3d(mx, my, mz, vx, vy, vz)
            local r, id = sampGetPlayerIdByCharHandle(v)
            if r and cDist < dist then
                dist = cDist
                nearId = id
            end
        end
    end
    return nearId
end

function player_handle_get()
    local id = getNearestID()
    if id then
        result, handle = sampGetCharHandleBySampPlayerId(id)
        sampAddChatMessage('nearest id '..id, -1)
    else
        -- Ближайший игрок не найден
    end
end


handle+id
можно ли сделать так, если игрок не найден в зоне стрима, то сообщение будет выводиться но вернёт id 0?
 
Последнее редактирование:

IlyaFontonov

Активный
177
41
Lua:
function main()
    if not isSampfuncsLoaded() or not isSampLoaded() then return end
    sampRegisterChatCommand("hplayer", player_handle_get)

    while true do wait(0) end
end


function getNearestID()
    local chars = getAllChars()
    local mx, my, mz = getCharCoordinates(PLAYER_PED)
    local nearId, dist = nil, 10000
    for i,v in ipairs(chars) do
        if doesCharExist(v) and v ~= PLAYER_PED then
            local vx, vy, vz = getCharCoordinates(v)
            local cDist = getDistanceBetweenCoords3d(mx, my, mz, vx, vy, vz)
            local r, id = sampGetPlayerIdByCharHandle(v)
            if r and cDist < dist then
                dist = cDist
                nearId = id
            end
        end
    end
    return nearId
end

function player_handle_get()
    local id = getNearestID()
    if id then
        result, handle = sampGetCharHandleBySampPlayerId(id)
        sampAddChatMessage('nearest id '..id, -1)
    else
        sampAddChatMessage('nearest id 0 (игрок в зоне стрима не найден)', -1)
    end
end
если в зоне стрима стоит 2+ игрока, то выводит айди обоих, несмотря что второй игрок дальше первого


можно ли сделать так, если игрок не найден в зоне стрима, то сообщение будет выводиться но вернёт id 0?
Как-то так?
 
  • Нравится
Реакции: $Mr.R1ch$

$Mr.R1ch$

Активный
Автор темы
330
51
Lua:
function main()
    if not isSampfuncsLoaded() or not isSampLoaded() then return end
    sampRegisterChatCommand("hplayer", player_handle_get)

    while true do wait(0) end
end


function getNearestID()
    local chars = getAllChars()
    local mx, my, mz = getCharCoordinates(PLAYER_PED)
    local nearId, dist = nil, 10000
    for i,v in ipairs(chars) do
        if doesCharExist(v) and v ~= PLAYER_PED then
            local vx, vy, vz = getCharCoordinates(v)
            local cDist = getDistanceBetweenCoords3d(mx, my, mz, vx, vy, vz)
            local r, id = sampGetPlayerIdByCharHandle(v)
            if r and cDist < dist then
                dist = cDist
                nearId = id
            end
        end
    end
    return nearId
end

function player_handle_get()
    local id = getNearestID()
    if id then
        result, handle = sampGetCharHandleBySampPlayerId(id)
        sampAddChatMessage('nearest id '..id, -1)
    else
        sampAddChatMessage('nearest id 0 (игрок в зоне стрима не найден)', -1)
    end
end

Как-то так?
я вообще думал через return сделать, но можно просто написать в самой функции, чё париться. Спасибо