Jump to content

How can I set the player on a random team?


FlyingSpoon

Recommended Posts

Posted
How can I set the player on a random team?

I have 2 teams.

math.random 

Do not yield your back to your enemy, might feel something strange in your ass.

Two things are infinite the universe and human stupidity and i'm not sure about the universe.

UF: IsTextInGridList | GetGridListRowIndexFromText | Table.removeValue | removeHex | dxDrawTriangle

Skype: SaSuki102 | About Me | Youtube channel | Lua Tips & Tricks | Lua Strings | Lua Tables | Lua Operators

Posted
math.random 
setPlayerTeam 

Example;

messages = {  
"Better luck next time", 
"Don't think you're so cool now, do you?", 
"Nice one, pal", 
"Your opinion is void"  
} 
  
function wastedMessage ( killer, weapon, bodypart ) 
    local randomID = math.random ( 1, #messages ) --get a random ID from the table 
    local randomMessage = messages[randomID] --use that to retrieve a message 
    outputChatBox ( randomMessage, 255, 0, 0 ) --output the message 
end 

Hahaha Are You Gang ? This is Spartan

  • Moderators
Posted

No.

local teamBlue = createTeam ( "Team Blue", 0, 0, 255 ) 
local teamRed = createTeam ( "Team Red", 255, 0, 0 ) 
  
local teams = {teamBlue,teamRed} 
  
local randomteam = teams[math.random(#teams)] 

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted

So Idk how to set the Teams >>

local teamBlue = createTeam ( "Team Blue", 0, 0, 255 ) 
local teamRed = createTeam ( "Team Red", 255, 0, 0 ) 
  
local teams = {teamBlue,teamRed} 
local randomteam = teams[math.random(#teams)] 
  
function onStart() 
setPlayerTeam ( source, randomteam ) 
end 
addEventHandler("onResourceStart", getRootElement(), onStart) 

Posted

Quite easy:

  
local teamBlue = createTeam ( "Team Blue", 0, 0, 255 ) 
local teamRed = createTeam ( "Team Red", 255, 0, 0 ) 
  
local teams = {teamBlue,teamRed} 
local randomteam = teams[math.random(#teams)] 
  
function setTeams() 
  for _, player in ipairs(getElementsByType("player")) do -- We are going to launch the script for every single player. 
    setPlayerTeam(player, randomteam) 
  end 
end 
addEventHandler("onResourceStart", getRootElement(), setTeams) 
  

YnVnhLx.png

'^ Just a beginner scripter.'

  • Moderators
Posted

Line 6 >local randomteam = teams[math.random(#teams)]<, must be placed between line 9 and 10.

Else it isn't random per player.

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted
Line 6 >local randomteam = teams[math.random(#teams)]<, must be placed between line 9 and 10.

Else it isn't random per player.

Ye I noticed that I fixed via skype :P

YnVnhLx.png

'^ Just a beginner scripter.'

Posted

Please, next time post solution for community

local teams = { 
createTeam ( "Team Blue", 0, 0, 255 ), 
createTeam ( "Team Red", 255, 0, 0 ) 
} 
addEventHandler("onResourceStart", root, 
    function () 
    local teams = { 
    [1] = createTeam ( "Team Blue", 0, 0, 255 ), 
    [2] = createTeam ( "Team Red", 255, 0, 0 ) 
    } 
        for k,v in ipairs(getElementsByType('player')) do 
        setPlayerTeam(v, teams[math.random(1,#teams])) 
        end 
    end 
) 

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