Jump to content

RANDOM NAME AND BLIP COLOR WHEN LEAVING TEAM


Function

Recommended Posts

function setTeamColor()
for _,player in ipairs(getElementsByType("player")) do
for _,blip in ipairs(getElementsByType("blip")) do
local team = getPlayerTeam(player)
local r,g,b = getTeamColor(team)    
if (team) then 
setPlayerNametagColor(player,r,g,b)
setBlipColor(blip,r,g,b,255)
      end
    end
  end
end
setTimer(setTeamColor,50,0)

function setRandomColor()
for _,player in ipairs(getElementsByType("player")) do
for _,blip in ipairs(getElementsByType("blip")) do
local team = getPlayerTeam(player)
local r,g,b = math.random(255),math.random(255),math.random(255)
if not (team) then
setPlayerNametagColor(player,r,g,b)                                                    
setBlipColor(blip,r,g,b,255)
      end
    end
  end
end
setTimer(setRandomColor,50,0)

Hi Guys, I have created a teamcolor script and it works very well when I enter into a team I get the color of the team in my name and blip.

But when I leave the team, the colors from my name and blip change all the time to random colors. I want to change it only once to a random color not all the time.

I think it is because the setTimer(setRandomColor,50,0)

Please can anyone help me :| ?

Edited by Function
Link to comment

Your setTimer executes every 50 ms, so when player isn't in any team his color is changing every 50 ms. Also you've put those timers outside of the function. It would be better to set the random color exactly when player is leaving the team.

Edited by Bananovy
Link to comment

If you've got your own team script then just set the color when you're using setPlayerTeam. Otherwise you have to make your own kind of detection, because there is no event such as onPlayerJoinTeam.

Quick Edit:

If your script is setting the team when player login, you can set his color at the same time.

Edited by Bananovy
Link to comment

How does player leave the team? Is he using any command or you do it manually?

Another edit:

If you just set the team once then use onPlayerLogin. You won't have to check every 50 ms if player is in the team or not.

Edited by Bananovy
Link to comment
  • Moderators
4 hours ago, Function said:
Spoiler


function setTeamColor()
for _,player in ipairs(getElementsByType("player")) do
for _,blip in ipairs(getElementsByType("blip")) do
local team = getPlayerTeam(player)
local r,g,b = getTeamColor(team)    
if (team) then 
setPlayerNametagColor(player,r,g,b)
setBlipColor(blip,r,g,b,255)
      end
    end
  end
end
setTimer(setTeamColor,50,0)

function setRandomColor()
for _,player in ipairs(getElementsByType("player")) do
for _,blip in ipairs(getElementsByType("blip")) do
local team = getPlayerTeam(player)
local r,g,b = math.random(255),math.random(255),math.random(255)
if not (team) then
setPlayerNametagColor(player,r,g,b)                                                    
setBlipColor(blip,r,g,b,255)
      end
    end
  end
end
setTimer(setRandomColor,50,0)

 

Hi Guys, I have created a teamcolor script and it works very well when I enter into a team I get the color of the team in my name and blip.

But when I leave the team, the colors from my name and blip change all the time to random colors. I want to change it only once to a random color not all the time.

I think it is because the setTimer(setRandomColor,50,0)

Please can anyone help me :| ?

As we don't have a onPlayerTeamChanged event (yet ?) you can rewrite your code to something like this:

function setTeamColor()
    for _,player in ipairs(getElementsByType("player")) do
        for _,blip in ipairs(getElementsByType("blip")) do
            local team = getPlayerTeam(player)
            if team then 
                local r,g,b = getTeamColor(team)
                setPlayerNametagColor(player,r,g,b)
                setBlipColor(blip,r,g,b,255)
                removeElementData(player, "isRandomColorAlreadySet") -- unmark as set (will get a new random color if he leaves the team)
            else -- if he doesn't have any team
                local isRandomColorAlreadySet = getElementData(player, "isRandomColorAlreadySet") -- check if marked as random color set
                if not isRandomColorAlreadySet then -- if not marked as random color set
                    setRandomColorFor(player) -- set random color
                end
            end
        end
    end
end
setTimer(setTeamColor,500,0) -- 500ms is still quick enough for what it does (lower the value back if you don't agree)

function setRandomColorFor( player )
    local r,g,b = math.random(255), math.random(255), math.random(255)
    setPlayerNametagColor(player,r,g,b)
    setBlipColor(blip,r,g,b,255)
    setElementData(player, "isRandomColorAlreadySet", true) -- mark as random color set
end

The player will get a new random color everytime he leaves a team, he reconnects, or the script/server restarts.

Edited by Citizen
Link to comment

Okay, I get it now. Well then looks like the best now you can do is to make a small "check" if player has his color already. I did it by using setElementData/getElementData:

function setRandomColor()
	for _,player in ipairs(getElementsByType("player")) do
		for _,blip in ipairs(getElementsByType("blip")) do
			local team = getPlayerTeam(player)
			local r,g,b = math.random(255),math.random(255),math.random(255)
			if not (team) then
				local gotHisColor = getElementData(player, "gotRandomColor") or false
				if gotHisColor == false then
					setPlayerNametagColor(player,r,g,b)                                                    
					setBlipColor(blip,r,g,b,255)
					setElementData(player, "gotRandomColor", true)
				end
			end
		end
	end
end
setTimer(setRandomColor,50,0)

 

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...