Jump to content

Random team selection


Recommended Posts

Posted (edited)

Like I posted before, this post is related to the previous one. So basically this code selects whether the person goes to Police or Drug dealer. I want random selection, I mean a player types /dd and he is selected in the team. The selection should be done 1 by 1 so there are equal number of players in each team.

 * fixed *  

Edited by Guest
  • Moderators
Posted
local theTeams = {"Drug Dealers", "Police"} 
  
  
  
  
  
  
addCommandHandler("dd", 
function(player) 
  
local team = getTeamFromName(theTeams[math.random(#theTeams)]) 
if team then -- does the team exist? 
  
setPlayerTeam(player, team) 
spawnPlayer(player, 1585.5485839844, -1677.8289794922, 5.8969912528992) 
setCameraTarget(player, player) 
fadeCamera(player, true) 
setElementModel(player, 280) 
giveWeapon(player, 22, 200) 
setPlayerNametagColor(player, 0, 0, 255) 
outputChatBox("#0000ff(Mission) #8e8c00Kill the #fff000Drug Lord #8e8c00before he delievers the drugs!", player, 255, 255, 255, true) 
  
else 
    outputDebugString("Team not found!") 
end 
  
end) 

Posted

What about the Drug Dealers team's properties? Your code will only select the teams randomly, I want 1 by 1 selection.

setPlayerTeam(player, getTeamFromName("Drug Dealers")) 
spawnPlayer(player, 1887.6721191406, -2473.6330566406, 13.53911781311) 
setCameraTarget(player, player) 
fadeCamera(player, true) 
setElementModel(player, math.random(1, 200)) 
giveWeapon(player, 22, 200) 
setPlayerNametagColor(player, 255, 0, 0) 
outputChatBox("#ff0000(Mission) #8e8c00Help the #fff000Drug Lord #8e8c00to deliever the drugs!", player, 255, 255, 255, true) 
end 

Posted

What about the Drug Dealers team's properties? Your code will only select the teams randomly, I want 1 by 1 selection.

setPlayerTeam(player, getTeamFromName("Drug Dealers")) 
spawnPlayer(player, 1887.6721191406, -2473.6330566406, 13.53911781311) 
setCameraTarget(player, player) 
fadeCamera(player, true) 
setElementModel(player, math.random(1, 200)) 
giveWeapon(player, 22, 200) 
setPlayerNametagColor(player, 255, 0, 0) 
outputChatBox("#ff0000(Mission) #8e8c00Help the #fff000Drug Lord #8e8c00to deliever the drugs!", player, 255, 255, 255, true) 
end 

  • Moderators
Posted

Try this:

local theTeams = {"Drug Dealers", "Police"} 
  
  
local sortAtPlayersInTeam = function (teamNameA,teamNameB) 
    local teamA, teamB = getTeamFromName(teamNameA), getTeamFromName(teamNameB) 
    if teamA and teamB then 
        local playersInTeamA = getPlayersInTeam (teamA) 
        local playersInTeamB = getPlayersInTeam (teamB) 
        return  playersInTeamA > playersInTeamB or (playersInTeamA == playersInTeamB and math.random(2) == 1) 
    end 
    return false 
end 
  
  
  
addCommandHandler("dd", 
function(player) 
  
    table.sort(theTeams, sortAtPlayersInTeam)   
    local team = getTeamFromName(theTeams[1]) 
     
    if team then -- does the team exist? 
      
        setPlayerTeam(player, team) 
        spawnPlayer(player, 1585.5485839844, -1677.8289794922, 5.8969912528992) 
        setCameraTarget(player, player) 
        fadeCamera(player, true) 
        setElementModel(player, 280) 
        giveWeapon(player, 22, 200) 
        setPlayerNametagColor(player, 0, 0, 255) 
        outputChatBox("#0000ff(Mission) #8e8c00Kill the #fff000Drug Lord #8e8c00before he delievers the drugs!", player, 255, 255, 255, true) 
          
    else 
        outputDebugString("Team not found!") 
    end 
  
end) 

  • Moderators
Posted

Try this:

local theTeams = {"Drug Dealers", "Police"} 
  
  
local sortAtPlayersInTeam = function (teamNameA,teamNameB) 
    local teamA, teamB = getTeamFromName(teamNameA), getTeamFromName(teamNameB) 
    if teamA and teamB then 
        local playersInTeamA = getPlayersInTeam (teamA) 
        local playersInTeamB = getPlayersInTeam (teamB) 
        return  playersInTeamA > playersInTeamB or (playersInTeamA == playersInTeamB and math.random(2) == 1) 
    end 
    return false 
end 
  
  
  
addCommandHandler("dd", 
function(player) 
  
    table.sort(theTeams, sortAtPlayersInTeam)   
    local team = getTeamFromName(theTeams[1]) 
     
    if team then -- does the team exist? 
      
        setPlayerTeam(player, team) 
        spawnPlayer(player, 1585.5485839844, -1677.8289794922, 5.8969912528992) 
        setCameraTarget(player, player) 
        fadeCamera(player, true) 
        setElementModel(player, 280) 
        giveWeapon(player, 22, 200) 
        setPlayerNametagColor(player, 0, 0, 255) 
        outputChatBox("#0000ff(Mission) #8e8c00Kill the #fff000Drug Lord #8e8c00before he delievers the drugs!", player, 255, 255, 255, true) 
          
    else 
        outputDebugString("Team not found!") 
    end 
  
end) 

  • Moderators
Posted
If the player is selected as a drug dealer, his properties will be set same as the army guy's.
local theTeams = {"Drug Dealers", "Police"} 
  
  
local sortAtPlayersInTeam = function (teamNameA,teamNameB) 
    local teamA, teamB = getTeamFromName(teamNameA), getTeamFromName(teamNameB) 
    if teamA and teamB then 
        local playersInTeamA = getPlayersInTeam (teamA) 
        local playersInTeamB = getPlayersInTeam (teamB) 
        return  playersInTeamA > playersInTeamB or (playersInTeamA == playersInTeamB and math.random(2) == 1) 
    end 
    return false 
end 
      
      
      
addCommandHandler("dd", 
function(player) 
  
    table.sort(theTeams, sortAtPlayersInTeam)   
    local teamName = theTeams[1] 
    local team = getTeamFromName(teamName) 
    
    if team then -- does the team exist? 
        if teamName ==  "Police" then  
            setPlayerTeam(player, getTeamFromName("Police")) 
            spawnPlayer(player, 1585.5485839844, -1677.8289794922, 5.8969912528992) 
            setCameraTarget(player, player) 
            fadeCamera(player, true) 
            setElementModel(player, 280) 
            giveWeapon(player, 22, 200) 
            setPlayerNametagColor(player, 0, 0, 255) 
            outputChatBox("#0000ff(Mission) #8e8c00Kill the #fff000Drug Lord #8e8c00before he delievers the drugs!", player, 255, 255, 255, true) 
        elseif teamName == "Drug Dealers" then 
            setPlayerTeam(player, team) 
            spawnPlayer(player, 1585.5485839844, -1677.8289794922, 5.8969912528992) 
            setCameraTarget(player, player) 
            fadeCamera(player, true) 
            setElementModel(player, 280) 
            giveWeapon(player, 22, 200) 
            setPlayerNametagColor(player, 0, 0, 255) 
            outputChatBox("#0000ff(Mission) #8e8c00Kill the #fff000Drug Lord #8e8c00before he delievers the drugs!", player, 255, 255, 255, true) 
        end 
    else 
        outputDebugString("Team not found!") 
    end 
end) 

  • Moderators
Posted
local playersInTeamA = #getPlayersInTeam (teamA) 
local playersInTeamB = #getPlayersInTeam (teamB) 

Posted

Random selection is working, but I want to disable the /dd and /lord when the player is already selected in a team and show an error, "You can not change your team now!".

  • Moderators
Posted
if getPlayerTeam(player) then 
--... 
else 
    outputChatBox("You can not change your team now!", player) 
end 

  • Moderators
Posted
addCommandHandler("dd", 
function(player) 
    if getPlayerTeam(player) then 
    -- the rest of the code. 
    else 
        outputChatBox("You can not change your team now!", player) 
    end 
end) 

  • Moderators
Posted
addCommandHandler("dd", 
function(player) 
    if not getPlayerTeam(player) then 
        table.sort(theTeams, sortAtPlayersInTeam)   
        local teamName = theTeams[1] 
        local team = getTeamFromName(teamName) 
    
        if team then -- does the team exist? 
            if teamName ==  "US Army" then 
                setPlayerTeam(player, getTeamFromName("US Army")) 
                spawnPlayer(player, 1585.5485839844, -1677.8289794922, 5.8969912528992) 
                setCameraTarget(player, player) 
                fadeCamera(player, true) 
                setElementModel(player, 163) 
                giveWeapon(player, 31, 500) 
                giveWeapon(player, 29, 200) 
                giveWeapon(player, 24, 100) 
                setPlayerNametagColor(player, 221, 177, 0) 
                blipua = createBlipAttachedTo(player, 0, 2, 221, 177, 0) 
                outputChatBox("#0000ff(Mission) #8e8c00Kill the #328c6eDrug Lord #8e8c00before he delievers the drugs!", player, 255, 255, 255, true) 
            elseif teamName == "Drug Dealers" then 
                setPlayerTeam(player, team) 
                spawnPlayer(player, 1484.75549, -666.62592, 95.60126) 
                setCameraTarget(player, player) 
                fadeCamera(player, true) 
                setElementModel(player, 29) 
                giveWeapon(player, 30, 500) --Give him a weapon pack 
                giveWeapon(player, 23, 50) 
                giveWeapon(player, 25, 30) 
                setPlayerNametagColor(player, 168, 0, 255) 
                blipdd = createBlipAttachedTo(player, 0, 2, 168, 0, 255) 
                outputChatBox("#008AB0(Mission) #8e8c00Help the #328c6eDrug Lord #8e8c00to deliever the drugs!", player, 255, 255, 255, true) 
            end 
        end 
    else 
        outputChatBox("#FF0000Error: You can not change your team now", player, 255, 255, 255, true) 
    end 
end) 

  • Moderators
Posted
Same problem. Selecting me in the team when I move myself to a team through admin panel and not selecting when I'm not in a team.

nope,

if [b][color=#FF0000]not[/color][/b] getPlayerTeam(player) then 
-- set in team 

Make sure you update the version correctly.

Posted

Yeah it's working. I posted the same comment 2 times. I was commenting and the comment wasn't appearing on the first page. I'm pretty new here so I thought the comments will continue on the same page. Sorry. :P

  • 3 weeks later...
  • Moderators
Posted
The script doesn't select 1 by 1, it is still selecting players randomly.

Selecting players randomly? You never said anything about random players, only (random + 1 by 1) adding player to team.

Which is doing that.

If you still have your doubts about that, why don't you check theTeams table?

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