Skip to content
This repository has been archived by the owner on Apr 7, 2023. It is now read-only.

update scoreboard to allow OneSync support. #150

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions resources/[system]/scoreboard/cl_scoreboard.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
local plyTable = {}

RegisterNetEvent('fivem:playerJoined', function(source, name)
plyTable[source] = name
end)

RegisterNetEvent('fivem:syncPlayerData', function(scoreTbl)
plyTable = scoreTbl
end)

RegisterNetEvent('fivem:playerLeft', function(source)
plyTable[source] = nil
end)

RegisterCommand('+scoreboard', function()
local localPly = PlayerPedId()
local players = {}
for serverId, name in pairs(plyTable) do
local ply = GetPlayerFromServerId(serverId)
local wantedLevel
local r, g, b = 255, 255, 255
-- if the player exists and isnt themselves.
if ply ~= localPly then
wantedLevel = GetPlayerWantedLevel(ply)
r, g, b = GetPlayerRgbColour(ply)
end
players[#players + 1] = '<tr style=\"color: rgb(' .. r .. ', ' .. g .. ', ' .. b .. ')\"><td>' .. serverId .. '</td><td>' .. sanitize(name) .. '</td><td>' .. (wantedLevel and wantedLevel or tostring(0)) .. '</td></tr>'
end
SendNUIMessage({ text = table.concat(players) })
end)
RegisterCommand('-scoreboard', function()
SendNUIMessage({
meta = 'close'
})
end)
RegisterKeyMapping('+scoreboard', 'Opens Scoreboard', 'keyboard', GetConvar('scoreboard_toggleScoreboard', 'UP'))

function sanitize(txt)
local replacements = {
['&' ] = '&amp;',
['<' ] = '&lt;',
['>' ] = '&gt;',
['\n'] = '<br/>'
}
return txt
:gsub('[&<>\n]', replacements)
:gsub(' +', function(s) return ' '..('&nbsp;'):rep(#s-1) end)
end
3 changes: 2 additions & 1 deletion resources/[system]/scoreboard/fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ repository 'https://github.com/citizenfx/cfx-server-data'
-- temporary!
ui_page 'html/scoreboard.html'

client_script 'scoreboard.lua'
client_script 'cl_scoreboard.lua'
server_script 'sv_scoreboard.lua'

files {
'html/scoreboard.html',
Expand Down
48 changes: 0 additions & 48 deletions resources/[system]/scoreboard/scoreboard.lua

This file was deleted.

17 changes: 17 additions & 0 deletions resources/[system]/scoreboard/sv_scoreboard.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- prevent the possibility of someone spamming this event to try and net lag the server.
local joinedTbl = {}
RegisterNetEvent('playerJoined', function()
local name = GetPlayerName(source)
if not joinedTbl[source] then
joinedTbl[source] = name
TriggerClientEvent('fivem:playerJoined', -1, source, name)
TriggerClientEvent('fivem:syncPlayerData', source, joinedTbl)
else
print(('%s [%s] tried sending the \'playerJoined\' net event, but they\'ve already joined!'):format(name, source))
end
end)

AddEventHandler('playerDropped', function()
joinedTbl[source] = nil
TriggerClientEvent('fivem:playerLeft', -1, source)
end)