Jump to content

Togging Global chat ON / OFF


Coffey

Recommended Posts

Hey! I have a global chat script. I want to make it so if someone types: /gcoff then he can't see the GCHAT. It doesn't necessary to save this data, but if anybody can help me make this, I'd really appreciate it!

Here is the code.

function globalMessage(thePlayer, cmd, ...) 
    local message = table.concat ( { ... }, " " ); 
    local name = getPlayerName(thePlayer); 
for _,v in ipairs(getElementsByType("player")) do 
    outputChatBox("#FF0000[GLOBAL]#00FF00"..name..": #FFFFFF"..message,v, 255, 255, 255, true) 
    end 
end 
addCommandHandler("global",  globalMessage); 
  
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), 
 function() 
  for index,player in pairs(getElementsByType("player")) do 
        bindKey(player,"b", "down", "chatbox", "global"); 
  end 
end 
) 
  
addEventHandler("onPlayerJoin",root, 
function () 
   bindKey(source,"b", "down", "chatbox", "global"); 
end 
); 

I would really appreciate your help!

Link to comment

In order to make something similar to your request, you should create, as Castillo said, a table holding every player as a row. Inside each row, you should place a variable depending on whether it's disabled (true) or not. By default, everything is false in a table. So, you create a commandHandler setting their table[player] equal to false.

That's done, one step more. You must add a if-check inside the function where you outputChatBox in order to send (or not) the message. Please, don't watch out the spoiler unless you tried and you couldn't do it.

local isChatDisabled = {} 
  
function globalMessage(thePlayer, cmd, ...) 
    local message = table.concat ( { ... }, " " ); 
    local name = getPlayerName(thePlayer); 
    for _,v in ipairs(getElementsByType("player")) do 
         if not isChatDisabled[v] then 
              outputChatBox("#FF0000[GLOBAL]#00FF00"..name..": #FFFFFF"..message,v, 255, 255, 255, true) 
         end 
    end 
end 
addCommandHandler("global",  globalMessage); 
  
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), 
 function() 
  for index,player in pairs(getElementsByType("player")) do 
        bindKey(player,"b", "down", "chatbox", "global"); 
  end 
end 
) 
  
addEventHandler("onPlayerJoin",root, 
function () 
   bindKey(source,"b", "down", "chatbox", "global"); 
end 
); 
  
addCommandHandler("gcoff", 
function(p) 
     isChatDisabled[p] = true; 
end ); 

Link to comment

Hey.

I've tried to make something like that, but it just didn't work.

I copied a bit from your code, but somehow if I type /gcoff, it really turns it off, but then I cant turn it on back...

Anyway, thank your help, I hope you can help me with this, I am really a big noob in LUA but I am trying hard. :D

Link to comment
Hey.

I've tried to make something like that, but it just didn't work.

I copied a bit from your code, but somehow if I type /gcoff, it really turns it off, but then I cant turn it on back...

Anyway, thank your help, I hope you can help me with this, I am really a big noob in LUA but I am trying hard. :D

Add another command handler to set isChatDisabled[p] to false

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