MrSecreto07 Posted June 12, 2017 Share Posted June 12, 2017 Hi guys again How can I create a chat script that is visible only for 4 teams? Someone can help me by telling me which commands should I use Link to comment
Scripting Moderators thisdp Posted June 12, 2017 Scripting Moderators Share Posted June 12, 2017 getPlayerTeam Link to comment
MrSecreto07 Posted June 13, 2017 Author Share Posted June 13, 2017 13 hours ago, thisdp said: getPlayerTeam Can you be a bit more specific? It's just that I'm new to LUA Link to comment
MrSecreto07 Posted June 13, 2017 Author Share Posted June 13, 2017 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
PeaceBomb99 Posted June 13, 2017 Share Posted June 13, 2017 (edited) 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 June 13, 2017 by eggman Link to comment
MrSecreto07 Posted June 14, 2017 Author Share Posted June 14, 2017 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
pa3ck Posted June 14, 2017 Share Posted June 14, 2017 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 thisdp Posted June 14, 2017 Scripting Moderators Share Posted June 14, 2017 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
pa3ck Posted June 14, 2017 Share Posted June 14, 2017 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
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