Jump to content

New chat


Razzer47

Recommended Posts

hey, this is an example. -> only players around you will be able to see your chat messages.

server-side.

  
local  maxDistance = 20;   -- 20 metres will be the maximum distance to see player messages. 
  
addEventHandler("onPlayerChat", getRootElement(), 
   function (message, messageType)  
        if messageType==1 then return; end; -- do nothing if message is an action message. 
         
        -- now, get players near the player who sent the message and store them in a table. 
        local nearestPlayers = {};  
        local players = getElementsByType("player", getRootElement());  
        local pX, pY, pZ = getElementPosition(source);  
        for _,player in ipairs(players) do  
             local sX, sY, sZ = getElementPosition(player); 
             if ((pX-sX)^2 + (pY-sY)^2 + (pZ-sZ)^2) <= maxDistance^2 then  
                    -- player is near, so, add it to the list... 
                    table.insert(nearestPlayers,player);  
             end;  
        end;  
  
        -- now, check if the message is a team message. in that case, we remove from the table all the players 
        -- that are not inside the team of the player who sendt the message. 
        local playerTeam = getPlayerTeam(source); 
        if playerTeam and (messageType==2) then  
             for index,player in ipairs(nearestPlayers) do  
                   if getPlayerTeam(player) ~= playerTeam then  
                          -- if teams are different, remove from list... 
                          table.remove(nearestPlayers, index);  
                   end;  
             end;  
        end;  
  
        -- now send the chat message for that players... 
        local chatMessage = getPlayerName(source) .. "#FFFFFF: " .. message; 
         
        for _,player in ipairs(nearestPlayers) do  
             outputChatBox(chatMessage, player, 255,255,255, true);  
        end;  
  
        cancelEvent(); -- cancel event so that the chat message will not be shown directly. 
   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...