Create thread to specific player

Hello everyone!

This one I’ve been looking for some time but I didn’t find an answer.

I’ve created a function to get all players in a specific radius. Now I want to display a UI to all thoose players, but only when the “local player” press a key.

Here is my code (client side script)

local ped
local radius = 20.0
local keybind = 20 -- Z (keyboard)

function GetPlayersInRadius()
    if not ped then return end
    
    local players = GetPlayers()
    local coords = GetEntityCoords(ped)

    for _,v in pairs(players) do
        local nearPed = GetPlayerPed(v)

        if nearPed ~= ped then
            local nearCoords = GetEntityCoords(nearPed)

            if GetDistanceBetweenCoords(coords.x, coords.y, coords.z, nearCoords.x, nearCoords.y, nearCoords.z, true) <= radius then
                return nearPed
            end
        end
    end

    return false
end

CreateThread(function()
    while true do
        Wait(0)
        ped = PlayerPedId()
        
        if IsControlJustPressed(0, keybind) then
            local nearPed = GetPlayersInRadius()
            
            if nearPed then
                SendNUIMessage({ var1: "test" })
                -- I want this UI to appear only to 'nearPed'
            end
        end
    end
end)

I believe I need to create a thread specific to that player… but how?

Thanks for reading! :smile:

I think you need something like this…


client.lua

local ped
local keybind = 20 -- Z (keyboard)

CreateThread(function()
    while true do
        Wait(0)
        ped = PlayerPedId()
        
        if IsControlJustPressed(0, keybind) then
			TriggerServerEvent('server:open_ui_to_nearests')
        end
    end
end)

RegisterNetEvent('client:openui', function()
	SendNUIMessage({ var1: "test" })
end)

server.lua

local radius = 20.0
RegisterNetEvent('server:open_ui_to_nearests', function(source)
	-- this send to all player in the radius
	local src = source
	local players = GetPlayers()
    local coords = GetEntityCoords(ped)

    local PlayerPed = GetPlayerPed(source)
    local PlayerCoords = GetEntityCoords(PlayerPed)

    for _, TargetId in pairs(players) do
        local nearPed = GetPlayerPed(TargetId)
        if nearPed ~= ped then
            local nearCoords = GetEntityCoords(nearPed)
			if #(PlayerCoords - nearCoords) <= radius then
				TriggerClientEvent('client:openui', TargetId)
			end
        end
    end
end)

It seems to work. I’ve made a few adjustments but the main is there. Thank you! :smile: