[HELP] GetPlayerPing on scoreboard


#1

Hi there !

Im trying to put players ping instead of wantedlevel in the scoreboard ressource. I cant get it work… (learning lua ^^)
I hope this post can help other people aswell.

The code:

Citizen.CreateThread(function()
    listOn = false
    while true do
        Wait(0)

        if IsControlPressed(0, 37)--[[ INPUT_SELECT_WEAPON ]] then
            if not listOn then
                local players = {}
                ptable = GetPlayers()
                for _, i in ipairs(ptable) do
                    local ping = GetPlayerPing(i)
                    r, g, b = GetPlayerRgbColour(i)
                    table.insert(players, 
                    '<tr style=\"color: rgb(' .. r .. ', ' .. g .. ', ' .. b .. ')\"><td>' .. GetPlayerServerId(i) .. '</td><td>' .. GetPlayerName(i) .. '</td><td>' .. ping .. '</td></tr>'
                    )
                end
                
                SendNUIMessage({ text = table.concat(players) })

                listOn = true
                while listOn do
                    Wait(0)
                    if(IsControlPressed(0, 37) == false) then
                        listOn = false
                        SendNUIMessage({
                            meta = 'close'
                        })
                        break
                    end
                end
            end
        end
    end
end)

The error:

Error resuming coroutine: scoreboard.lua:13: attempt to call a nil value (global ‘GetPlayerPing’)
stack traceback:
scoreboard.lua:13 in function <scoreboard.lua3>


#2

I am finding that ‘GetPlayerPing’ is not something that called from a client script but rather from a server side function.

It may be possible to call the server function and output it to the client script.

I could be wrong however.


#3

Exactly.
You can’t call GetPlayerPing on client, you have to do that on the server.
However a client could ask the server for a specific ping with some event and that event would send back to that client the ping of the user :slight_smile:


#4

Do you have an example of how to do that? I too am looking for this function. I was just throwing out what I have already learned to see if it would help him.

Here is what I have so far:

Amending Scoreboard.lua:

TriggerServerEvent('checkPlayerPing')
                    AddEventHandler("checkPlayerPing", function()
                    local ping = GetPlayerPing(i)
                    table.insert(players, 
                    '<tr style=\"color: rgb(' .. r .. ', ' .. g .. ', ' .. b .. ')\"><td>' .. GetPlayerServerId(i) .. '</td><td>' .. GetPlayerName(i) .. '</td><td>' .. ping(i) .. '</td></tr>'
                    )
                end

Then created a server.lua with:

RegisterServerEvent("checkPlayerPing")
AddEventHandler("checkPlayerPing", function()
	ping = GetPlayerPing(source)

	end)

I have no idea if I am even close. Help would be appreciated for both the OP and myself.

Thanks!


#5

You’re kinda doing it completely wrong with the code. :smiley:
Something like this:
Client:

TriggerServerEvent("checkPlayerPing", clientid, playerid)

RegisterNetEvent("playerPingResult")
AddEventHandler("playerPingResult", function(id, ping)
 --Do whatever you want with the id of that player and his ping :)
end)

Server:

RegisterServerEvent("checkPlayerPing", function(reqclient, player)
 TriggerClientEvent("playerPingResult", reqclient, player, GetPlayerPing(player))
end)

for clientid needs to be the id of the user that is requesting the player ping check (opening scoreboard in this case) and the playerid is the id of the layer that he wants the ping.
Not sure if there is a better way, because I myself am just 1-2 weeks into this, but I hope I could help. :slight_smile:


#6

I tried using your script and modified it, but still cant get it work… I going to get crazy ! :rage: