Jump to content

Multiple Team chat


Recommended Posts

I created this code:

function RadioChat ( thePlayer, cmd, ... )
	local theTeam = getTeamFromName ( "Team1") 
	if ( theTeam ) then
	local message = table.concat ( { ... }, " " )
	  local name = getPlayerName(thePlayer)
		local players = getPlayersInTeam ( theTeam )
		for playerKey, playerValue in ipairs ( players ) do
 outputChatBox("(*RADIO*)  " .. getPlayerName ( thePlayer  ) .. " #ffffff"..message, playerValue, 0, 0, 255, true)
		end
	end
end
addCommandHandler ( "radio", RadioChat )

It's a chat radio that I want to be visible only to 4 teams. The only thing I lack is to be able to add the other teams, I tried several ways and they do not work. 

 

Try these and they did not work:

local theTeam = getTeamFromName ( "Team1", "Team2", "Team3", "Team4") 
local theTeam = (getTeamFromName ( "Team1")) or (getTeamFromName ( "Team2")) or (getTeamFromName ( "Team3")) or (getTeamFromName ( "Team4")) 

someone help me?
 

Link to comment

You have to loop through your teams

local teams = {"Team1", "Team2", "Team3", "Team4"}

function RadioChat(thePlayer, _, ...)
	local message = table.concat ( { ... }, " " )
	for i=1, #teams do
		local team = getTeamFromName(teams[i]) -- teams[i] means, teams[1], teams[2], teams[3], teams[4] (returns the value for each)
		if team then
			local name = getPlayerName(thePlayer)
			local players = getPlayersInTeam ( theTeam )
			for playerKey, playerValue in ipairs ( players ) do
				outputChatBox("(*RADIO*)  " .. getPlayerName ( thePlayer  ) .. " #ffffff"..message, playerValue, 0, 0, 255, true)
			end
		end
	end
end

Basically does for each value in teams table execute the code. Hope  this helps.

 

 

Edited by eggman
Link to comment
16 hours ago, eggman said:

You have to loop through your teams


local teams = {"Team1", "Team2", "Team3", "Team4"}

function RadioChat(thePlayer, _, ...)
	local message = table.concat ( { ... }, " " )
	for i=1, #teams do
		local team = getTeamFromName(teams[i]) -- teams[i] means, teams[1], teams[2], teams[3], teams[4] (returns the value for each)
		if team then
			local name = getPlayerName(thePlayer)
			local players = getPlayersInTeam ( theTeam )
			for playerKey, playerValue in ipairs ( players ) do
				outputChatBox("(*RADIO*)  " .. getPlayerName ( thePlayer  ) .. " #ffffff"..message, playerValue, 0, 0, 255, true)
			end
		end
	end
end

Basically does for each value in teams table execute the code. Hope  this helps.

 

 

Is worked the teams, thanks

But I noticed a small error, the players that are not in one of those teams can use the command, they do not see anything in the chat but those who are in the teams if they can see what those players write.

Link to comment
local teams = {"Team1", "Team2", "Team3", "Team4"}

function RadioChat(thePlayer, _, ...)
  	if not isPlayerInAllowedTeam(thePlayer) then
		outputChatBox("You are not in the list of allowed teams", thePlayer)
		return
   	end
	local message = table.concat ( { ... }, " " )
	for i=1, #teams do
		local team = getTeamFromName(teams[i]) -- teams[i] means, teams[1], teams[2], teams[3], teams[4] (returns the value for each)
		if team then
			local name = getPlayerName(thePlayer)
			local players = getPlayersInTeam ( theTeam )
			for playerKey, playerValue in ipairs ( players ) do
				outputChatBox("(*RADIO*)  " .. getPlayerName ( thePlayer  ) .. " #ffffff"..message, playerValue, 0, 0, 255, true)
			end
		end
	end
end

function isPlayerInAllowedTeam(player)
  local isAllowed = false
  for k, v in ipairs(teams) do
    if getPlayerTeam(player) == getTeamFromName(v) then
      isAllowed = true
      break
    end
  end
  return isAllowed
end

That should solve it

Link to comment
  • Scripting Moderators
1 hour ago, pa3ck said:

local teams = {"Team1", "Team2", "Team3", "Team4"}

function RadioChat(thePlayer, _, ...)
  	if not isPlayerInAllowedTeam(thePlayer) then
		outputChatBox("You are not in the list of allowed teams", thePlayer)
		return
   	end
	local message = table.concat ( { ... }, " " )
	for i=1, #teams do
		local team = getTeamFromName(teams[i]) -- teams[i] means, teams[1], teams[2], teams[3], teams[4] (returns the value for each)
		if team then
			local name = getPlayerName(thePlayer)
			local players = getPlayersInTeam ( theTeam )
			for playerKey, playerValue in ipairs ( players ) do
				outputChatBox("(*RADIO*)  " .. getPlayerName ( thePlayer  ) .. " #ffffff"..message, playerValue, 0, 0, 255, true)
			end
		end
	end
end

function isPlayerInAllowedTeam(player)
  local isAllowed = false
  for k, v in ipairs(teams) do
    if getPlayerTeam(player) == getTeamFromName(v) then
      isAllowed = true
      break
    end
  end
  return isAllowed
end

That should solve it

GOOD. But you should take the performance into account.

Link to comment

It's only couple of lines of code, the loop is pretty small as well (probably between 10 and 50, don't think there would be more), couple of ms and it's done... But if you are that concerned about performance, you can try this:

local teams = {"Team1", "Team2", "Team3", "Team4"}
local allowedPlayers = {}

addEventHandler("onPlayerQuit", root, function()
    if allowedPlayers[source] then
      allowedPlayers[source] = nil
    end
end)

function RadioChat(thePlayer, _, ...)
  	if not allowedPlayers[thePlayer] or not isPlayerInAllowedTeam(thePlayer) then
		outputChatBox("You are not in the list of allowed teams", thePlayer)
		return
   	end
	local message = table.concat ( { ... }, " " )
	for i=1, #teams do
		local team = getTeamFromName(teams[i]) -- teams[i] means, teams[1], teams[2], teams[3], teams[4] (returns the value for each)
		if team then
			local name = getPlayerName(thePlayer)
			local players = getPlayersInTeam ( theTeam )
			for playerKey, playerValue in ipairs ( players ) do
				outputChatBox("(*RADIO*)  " .. getPlayerName ( thePlayer  ) .. " #ffffff"..message, playerValue, 0, 0, 255, true)
			end
		end
	end
end

function isPlayerInAllowedTeam(player)
  local isAllowed = false
  for k, v in ipairs(teams) do
    if getPlayerTeam(player) == getTeamFromName(v) then
      isAllowed = true
      if not allowedPlayers[player] then
        allowedPlayers[player] = true
      end
      break
    end
  end
  return isAllowed
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...