Jump to content

Teamcolor problem


Recommended Posts

I have this resource which allow me when i add Player into the team he gets the team color but not working please help :'(

function RGBToHex(red, green, blue, alpha) 
    if((red < 0 or red > 255 or green < 0 or green > 255 or blue < 0 or blue > 255) or (alpha and (alpha < 0 or alpha > 255))) then 
        return nil 
    end 
    if(alpha) then 
        return string.format("#%.2X%.2X%.2X%.2X", red,green,blue,alpha) 
    else 
        return string.format("#%.2X%.2X%.2X", red,green,blue) 
    end 
end 
  
local blip = {} 
  
local randomColor = { 
 [1] = {255,255,255}, 
 [2] = {255,0,255}, 
 [3] = {0,255,0} 
} 
  
addEventHandler("onPlayerTeamChange", root, 
function () 
     if not isElement(blip[source]) then 
        blip[source] = createBlipAttachedTo(source) 
    end 
    local theTeam = getPlayerTeam(source) 
    if theTeam then 
        local r, g, b = getTeamColor(theTeam) 
        setPlayerNametagColor(source, r, g, b) 
        setBlipColor(blip[source], r, g, b, 255) 
  setPlayerName(source,getPlayerName(source):gsub('#%x%x%x%x%x%x',RGBToHex(r,g,b))) 
    else 
        outputChatBox("YOU HAVE NO TEAM", source) 
  rr,gg,bb = unpack(randomColor[math.random(#randomColor)]) 
  setBlipColor(blip[source], rr,gg,bb,255) 
  setPlayerName(source,getPlayerName(source):gsub('#%x%x%x%x%x%x',RGBToHex(rr,gg,bb))) 
        setPlayerNametagColor(source, rr,gg,bb) 
  end 
 end 
) 
  
addEventHandler("onPlayerQuit", root, 
function() 
    if isElement(blip[source]) then 
        destroyElement(blip[source]) 
  end 
 end 
) 

Link to comment

"onPlayerTeamChange" is a non-existent event. You're going to need to make a custom one by following Cheez3D's solution;However, You need to find the script where you set the player's team ( where you use setPlayerTeam ) and trigger your new custom event "onPlayerTeamChange".

Link to comment

Go to the admin resource, then open admin_server.lua:

Paste Cheez3D's function into there:

local _setPlayerTeam = setPlayerTeam; 
setPlayerTeam = function(player,team) 
    local PreviousTeam = getPlayerTeam(player); 
    local Result = _setPlayerTeam(player,team); 
    if Result then 
        triggerEvent("onPlayerTeamChange",player,PreviousTeam,team); 
    end; 
    return Result; 
end; 

Then put this in YOUR script:

addEvent("onPlayerTeamChange"); 

I'm not sure if this will cause issues in the admin resource, but likely not as it's just triggering an event.

Link to comment

you mean in my script ?

function RGBToHex(red, green, blue, alpha) 
    if((red < 0 or red > 255 or green < 0 or green > 255 or blue < 0 or blue > 255) or (alpha and (alpha < 0 or alpha > 255))) then 
        return nil 
    end 
    if(alpha) then 
        return string.format("#%.2X%.2X%.2X%.2X", red,green,blue,alpha) 
    else 
        return string.format("#%.2X%.2X%.2X", red,green,blue) 
    end 
end 
  
local blip = {} 
  
local randomColor = { 
 [1] = {255,255,255}, 
 [2] = {255,0,255}, 
 [3] = {0,255,0} 
} 
  
addEventHandler("onPlayerTeamChange", root, 
function () 
     if not isElement(blip[source]) then 
        blip[source] = createBlipAttachedTo(source) 
    end 
    local theTeam = getPlayerTeam(source) 
    if theTeam then 
        local r, g, b = getTeamColor(theTeam) 
        setPlayerNametagColor(source, r, g, b) 
        setBlipColor(blip[source], r, g, b, 255) 
  setPlayerName(source,getPlayerName(source):gsub('#%x%x%x%x%x%x',RGBToHex(r,g,b))) 
    else 
        outputChatBox("YOU HAVE NO TEAM", source) 
  rr,gg,bb = unpack(randomColor[math.random(#randomColor)]) 
  setBlipColor(blip[source], rr,gg,bb,255) 
  setPlayerName(source,getPlayerName(source):gsub('#%x%x%x%x%x%x',RGBToHex(rr,gg,bb))) 
        setPlayerNametagColor(source, rr,gg,bb) 
  end 
 end 
) 
  
addEventHandler("onPlayerQuit", root, 
function() 
    if isElement(blip[source]) then 
        destroyElement(blip[source]) 
  end 
 end 
) 

Link to comment

No.. Follow the instructions I posted.

Go to the admin resource, open admin_server.lua and paste the code below into there:

local _setPlayerTeam = setPlayerTeam; 
setPlayerTeam = function(player,team) 
    local PreviousTeam = getPlayerTeam(player); 
    local Result = _setPlayerTeam(player,team); 
    if Result then 
        triggerEvent("onPlayerTeamChange",player,PreviousTeam,team); 
    end; 
    return Result; 
end; 

THEN go to your script, and paste the code below right before addEventHandler("onPlayerTeamChange"):

addEvent("onPlayerTeamChange"); 

Link to comment

like that ?

function RGBToHex(red, green, blue, alpha) 
    if((red < 0 or red > 255 or green < 0 or green > 255 or blue < 0 or blue > 255) or (alpha and (alpha < 0 or alpha > 255))) then 
        return nil 
    end 
    if(alpha) then 
        return string.format("#%.2X%.2X%.2X%.2X", red,green,blue,alpha) 
    else 
        return string.format("#%.2X%.2X%.2X", red,green,blue) 
    end 
end 
  
local blip = {} 
  
local randomColor = { 
 [1] = {255,255,255}, 
 [2] = {255,0,255}, 
 [3] = {0,255,0} 
} 
 addEvent("onPlayerTeamChange"); 
function () 
     if not isElement(blip[source]) then 
        blip[source] = createBlipAttachedTo(source) 
    end 
    local theTeam = getPlayerTeam(source) 
    if theTeam then 
        local r, g, b = getTeamColor(theTeam) 
        setPlayerNametagColor(source, r, g, b) 
        setBlipColor(blip[source], r, g, b, 255) 
  setPlayerName(source,getPlayerName(source):gsub('#%x%x%x%x%x%x',RGBToHex(r,g,b))) 
    else 
        outputChatBox("YOU HAVE NO TEAM", source) 
  rr,gg,bb = unpack(randomColor[math.random(#randomColor)]) 
  setBlipColor(blip[source], rr,gg,bb,255) 
  setPlayerName(source,getPlayerName(source):gsub('#%x%x%x%x%x%x',RGBToHex(rr,gg,bb))) 
        setPlayerNametagColor(source, rr,gg,bb) 
  end 
 end 
) 
  
addEventHandler("onPlayerQuit", root, 
function() 
    if isElement(blip[source]) then 
        destroyElement(blip[source]) 
  end 
 end 
) 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...