Function Posted March 14, 2017 Share Posted March 14, 2017 (edited) 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 March 14, 2017 by Function Link to comment
Bananovy Posted March 14, 2017 Share Posted March 14, 2017 (edited) 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 March 14, 2017 by Bananovy Link to comment
Function Posted March 14, 2017 Author Share Posted March 14, 2017 And how can I do this please :(? Link to comment
Bananovy Posted March 14, 2017 Share Posted March 14, 2017 (edited) 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 March 14, 2017 by Bananovy Link to comment
Function Posted March 14, 2017 Author Share Posted March 14, 2017 Can you edit my script correctly as you mean please Link to comment
Bananovy Posted March 14, 2017 Share Posted March 14, 2017 (edited) 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 March 14, 2017 by Bananovy Link to comment
Function Posted March 14, 2017 Author Share Posted March 14, 2017 Manually on the Admin Panel "Remove player from team" or when the player logout, because i am using a teamssaver too Link to comment
Bananovy Posted March 14, 2017 Share Posted March 14, 2017 Okay, so how do you set team for a player? Link to comment
Function Posted March 14, 2017 Author Share Posted March 14, 2017 (edited) Also with the admin panel Edited March 14, 2017 by Function Link to comment
Moderators Citizen Posted March 14, 2017 Moderators Share Posted March 14, 2017 (edited) 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 March 14, 2017 by Citizen Link to comment
Bananovy Posted March 14, 2017 Share Posted March 14, 2017 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
Function Posted March 15, 2017 Author Share Posted March 15, 2017 I think it change the namecolor of all players because I used getElementsByType("player") - all players. If (team) then setPlayerNametagColor. Guys do I need to use getPlayersInTeam or not? Do I need to use getPlayersInTeam or no Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now