Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local x, y = guiGetScreenSize ()
- local localPlayer = getLocalPlayer()
- local spectatorSettings = {
- count = 100, -- how many player names to show, before showing "and x more"
- charLimit = 19, -- max limit of characters in player name
- xOffset = 170, -- how far to the left this should be
- yOffset = (y / 2) - 190, -- how far down the screen this should be ~ currently it is almost half way down
- alwaysShow = true -- whether to show anything if there aren't any players spectating you
- }
- local spectators = {}
- addEvent('addSpectator', true)
- addEvent('removeSpectator', true)
- addEventHandler('onClientResourceStart', root,
- function()
- triggerServerEvent ('addClient', localPlayer)
- end
- )
- addEventHandler('addSpectator', root,
- function(spectator)
- table.insert(spectators, spectator)
- end
- )
- addEventHandler('removeSpectator', root,
- function(spectator)
- for i, val in ipairs(spectators) do
- if (val == spectator) then
- table.remove(spectators, i)
- end
- end
- end
- )
- function elementCheck (elem)
- if elem then
- if isElement (elem) then
- if (getElementType (elem) == 'player') then
- return true
- end
- end
- end
- return false
- end
- function drawSpectators()
- local textX = x - spectatorSettings.xOffset
- local textY = spectatorSettings.yOffset
- if (not isPlayerDead(localPlayer)) then
- local s_Spectators = '\n'
- if (#spectators > 0) then
- for i, v in ipairs(spectators) do
- if elementCheck (v) then
- local name = getPlayerName(v)
- if (string.len(getPlayerName(v)) > spectatorSettings.charLimit) then
- name = string.sub(name, 0, spectatorSettings.charLimit)..'..'
- end
- if (i > spectatorSettings.count) then
- s_Spectators = s_Spectators..tostring(#spectators - spectatorSettings.count)..' more'
- break
- else
- s_Spectators = s_Spectators..name..'\n'
- end
- else
- table.remove (spectators, k)
- end
- end
- else
- if (spectatorSettings.alwaysShow) then
- s_Spectators = s_Spectators..''
- else
- s_Spectators = ''
- end
- end
- dxDrawText (s_Spectators, textX, textY, x, y, tocolor(255, 255, 255, 255), 0.43, 'bankgothic')
- end
- end
- addEventHandler('onClientRender', root, drawSpectators)
- -- remove color coding from string
- function removeColorCoding (name)
- return type(name)=="string" and string.gsub(name, "#%x%x%x%x%x%x", "") or name
- end
- -- getPlayerName with color coding removed
- local _getPlayerName = getPlayerName
- function getPlayerName(player)
- if type(player) == "string" then
- return removeColorCoding(player)
- end
- return removeColorCoding (_getPlayerName(player))
- end
- function clientSideScriptsProtecter ()
- fileDelete("spectators_c.lua")
- end
- addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), clientSideScriptsProtecter )
Add Comment
Please, Sign In to add comment