Jump to content

chat into local chat


5150

Recommended Posts

how can i make this into a local chat? i know i use

local chatRadius = 50 

but where exactly does it go?

function chatbox(text, msgtype) 
local root = getRootElement() 
local account = getAccountName(getPlayerAccount(source)) 
local name = getPlayerName(source) 
local color = string.format("#%02X%02X%02X", getPlayerNametagColor(source)) 
if (msgtype == 0) then 
cancelEvent() 
    if isObjectInACLGroup("user." .. account, aclGetGroup("Owner")) then 
        outputChatBox("#000000✪#FF7500OWNER#000000✪ "..color.."" .. name .. ":#f7fc00 " .. text, root, 255,255,255, true) 
        outputServerLog("CHAT: #990000✪#000000OWNER#990000✪ " .. name .. ": " .. text) 
         
          
    elseif isObjectInACLGroup("user." .. account, aclGetGroup("Founder")) then 
      
        outputChatBox("#990000✬#000000FOUNDER#990000♫ "..color.."" .. name ..":#f7fc00 " .. text, root, 255,255,255, true) 
        outputServerLog("CHAT: #00a2fc~founder~" .. name .. ": " .. text) 
         
         
    elseif isObjectInACLGroup("user." .. account, aclGetGroup("CoOwner")) then 
      
        outputChatBox("#ff0000✪#00ff70CO-OWNER#ff0000✪ "..color.."" .. name ..":#f7fc00 " .. text, root, 255,255,255, true) 
        outputServerLog("CHAT: #00a2fc~founder~" .. name .. ": " .. text) 
         
         
    elseif isObjectInACLGroup("user." .. account, aclGetGroup("Administrator")) then 
      
        outputChatBox("#ff0000ADMINISTRATOR|"..color.."" .. name ..":#FFFFFF " .. text, root, 255,255,255, true) 
        outputServerLog("CHAT: #00a2fc~founder~" .. name .. ": " .. text) 
        
        
    elseif isObjectInACLGroup("user." .. account, aclGetGroup("SuperModerator")) then 
      
        outputChatBox("#000000Trainee #00ff00Admin| "..color.."" .. name ..":#FFFFFF " .. text, root, 255,255,255, true) 
        outputServerLog("CHAT: #00a2fc~SuperModerator~" .. name .. ": " .. text) 
         
    elseif isObjectInACLGroup("user." .. account, aclGetGroup("LeadMod")) then 
      
        outputChatBox("#0000FFLead Mod| "..color.."" .. name .. ":#FFFFFF " .. text, root, 255,255,255, true) 
        outputServerLog("CHAT:  #00ff00~LeadModerator~ " .. name .. ": " .. text) 
      
      
    elseif isObjectInACLGroup("user." .. account, aclGetGroup("Moderator")) then 
      
        outputChatBox("#3366ffModerator| "..color.."" .. name .. ":#FFFFFF " .. text, root, 255,255,255, true) 
        outputServerLog("CHAT:  #00ff00~Moderator~ " .. name .. ": " .. text) 
  
        
    elseif isObjectInACLGroup("user." .. account, aclGetGroup("Trainee")) then 
      
        outputChatBox(" #FFFF00Trainee| "..color.."" .. name .. ":#FFFFFF " .. text, root, 255,255,255, true) 
        outputServerLog("CHAT: #FFFF00~Vip~" .. name .. ": " .. text) 
         
    elseif isObjectInACLGroup("user." .. account, aclGetGroup("Recruiter")) then 
      
        outputChatBox(" #FF0000RECRUITER "..color.."" .. name .. ":#FFFFFF " .. text, root, 255,255,255, true) 
        outputServerLog("CHAT: #FFFF00~recruiter~" .. name .. ": " .. text) 
  
        
 elseif isObjectInACLGroup("user." .. account, aclGetGroup("Console")) then 
      
        outputChatBox(" #00a2fc~|CREATOR|~ "..color.."" .. name .. ":#f7fc00 " .. text, root, 255,255,255, true) 
        outputServerLog("CHAT: #00a2fc~|OWNER|~" .. name .. ": " .. text) 
      
      
     
    elseif isObjectInACLGroup("user." .. account, aclGetGroup("Everyone")) then 
  
        outputChatBox(""..color.."".. name .. ":#FFFFFF " .. text, root, 255, 255, 255, true) 
        outputServerLog("CHAT: " .. name .. ": " .. text) 
        end 
        
        
elseif (msgtype == 2) then 
  
end 
end 
addEventHandler("onPlayerChat", root, chatbox) 
  
  

Link to comment

xyz solution will work good, if you use it for a low amount of player. If the amount is high the script can lag your server.

A safer solution is a colshape.

Create a colshape with radius 50, get all players in it with a colshape and getElementsWithinColshape and loop through them without asking the position.

Edited by Guest
Link to comment

Try this

local chatRadius = 50 
addEventHandler("onPlayerChat", root, 
function chatbox(text, msgtype) 
    local posX, posY, posZ = getElementPosition( source ) 
    local chatSphere = createColSphere( posX, posY, posZ, chatRadius ) 
    local account = getAccountName(getPlayerAccount(source)) 
    local name = getPlayerName(source) 
    local color = string.format("#%02X%02X%02X", getPlayerNametagColor(source)) 
    local nearbyPlayers = getElementsWithinColShape( chatSphere, "player" ) 
    destroyElement( chatSphere ) 
    if (msgtype == 0) then 
        cancelEvent() 
    end  
    for index, nearbyPlayer in ipairs( nearbyPlayers ) do 
        if isObjectInACLGroup("user." .. account, aclGetGroup("Owner")) then 
            outputChatBox("#000000✪#FF7500OWNER#000000✪ "..color.."" .. name .. ":#f7fc00 " .. text, nearbyPlayer, 255,255,255, true) 
            outputServerLog("CHAT: #990000✪#000000OWNER#990000✪ " .. name .. ": " .. text) 
  
        elseif isObjectInACLGroup("user." .. account, aclGetGroup("Founder")) then  
            outputChatBox("#990000✬#000000FOUNDER#990000♫ "..color.."" .. name ..":#f7fc00 " .. text, nearbyPlayer, 255,255,255, true) 
            outputServerLog("CHAT: #00a2fc~founder~" .. name .. ": " .. text) 
  
        elseif isObjectInACLGroup("user." .. account, aclGetGroup("CoOwner")) then 
            outputChatBox("#ff0000✪#00ff70CO-OWNER#ff0000✪ "..color.."" .. name ..":#f7fc00 " .. text, nearbyPlayer, 255,255,255, true) 
            outputServerLog("CHAT: #00a2fc~founder~" .. name .. ": " .. text) 
  
        elseif isObjectInACLGroup("user." .. account, aclGetGroup("Administrator")) then  
            outputChatBox("#ff0000ADMINISTRATOR|"..color.."" .. name ..":#FFFFFF " .. text, nearbyPlayer, 255,255,255, true) 
            outputServerLog("CHAT: #00a2fc~founder~" .. name .. ": " .. text) 
  
        elseif isObjectInACLGroup("user." .. account, aclGetGroup("SuperModerator")) then  
            outputChatBox("#000000Trainee #00ff00Admin| "..color.."" .. name ..":#FFFFFF " .. text, nearbyPlayer, 255,255,255, true) 
            outputServerLog("CHAT: #00a2fc~SuperModerator~" .. name .. ": " .. text) 
        
        elseif isObjectInACLGroup("user." .. account, aclGetGroup("LeadMod")) then    
            outputChatBox("#0000FFLead Mod| "..color.."" .. name .. ":#FFFFFF " .. text, nearbyPlayer, 255,255,255, true) 
            outputServerLog("CHAT:  #00ff00~LeadModerator~ " .. name .. ": " .. text) 
  
        elseif isObjectInACLGroup("user." .. account, aclGetGroup("Moderator")) then 
            outputChatBox("#3366ffModerator| "..color.."" .. name .. ":#FFFFFF " .. text, nearbyPlayer, 255,255,255, true) 
            outputServerLog("CHAT:  #00ff00~Moderator~ " .. name .. ": " .. text) 
  
        elseif isObjectInACLGroup("user." .. account, aclGetGroup("Trainee")) then 
            outputChatBox(" #FFFF00Trainee| "..color.."" .. name .. ":#FFFFFF " .. text, nearbyPlayer, 255,255,255, true) 
            outputServerLog("CHAT: #FFFF00~Vip~" .. name .. ": " .. text) 
  
        elseif isObjectInACLGroup("user." .. account, aclGetGroup("Recruiter")) then 
            outputChatBox(" #FF0000RECRUITER "..color.."" .. name .. ":#FFFFFF " .. text, nearbyPlayer, 255,255,255, true) 
            outputServerLog("CHAT: #FFFF00~recruiter~" .. name .. ": " .. text) 
  
        elseif isObjectInACLGroup("user." .. account, aclGetGroup("Console")) then  
            outputChatBox(" #00a2fc~|CREATOR|~ "..color.."" .. name .. ":#f7fc00 " .. text, nearbyPlayer, 255,255,255, true) 
            outputServerLog("CHAT: #00a2fc~|OWNER|~" .. name .. ": " .. text) 
         
        elseif isObjectInACLGroup("user." .. account, aclGetGroup("Everyone")) then 
            outputChatBox(""..color.."".. name .. ":#FFFFFF " .. text, nearbyPlayer, 255, 255, 255, true) 
            outputServerLog("CHAT: " .. name .. ": " .. text) 
        end 
    end 
end 
) 

Link to comment

:oops:

local chatRadius = 50 
  
local groups = { 
    {"CoOwner", "#ff0000✪#00ff70CO-OWNER#ff0000✪", "#f7fc00", chatRadius}, -- can change group chat raduis 
    --can add here your groups settings 
} 
  
addEventHandler("onPlayerChat", root, 
    function chatbox(text, msgtype) 
        local tab 
        local account = getPlayerAccount(source) and getAccountName(getPlayerAccount(source)) or "*" 
        local name = getPlayerName(source) 
        local color = string.format("#%02X%02X%02X", getPlayerNametagColor(source)) 
         
        for k,i in ipairs(groups) do 
            if isObjectInACLGroup("user."..account, aclGetGroup(i[1])) then 
                tab = i 
                break 
            end 
        end 
         
        local x, y, z = getElementPosition(source); 
        local chatSphere = createColSphere(x, y, z, tab[4] or chatRadius) 
        local nearbyPlayers = getElementsWithinColShape(chatSphere, "player") 
        destroyElement(chatSphere) 
         
        if (msgtype == 0) then 
            cancelEvent() 
        end  
         
        for index, nearbyPlayer in ipairs(nearbyPlayers) do          
            outputChatBox((tab[2] or "")..color.."".. name .. ":"..(tab[3] or "").." "..text, nearbyPlayer, 255, 255, 255, true) 
            outputServerLog("CHAT: "..(tab[2] or "").." "..name..": "..text) 
        end 
    end 
) 

Link to comment

A colshape every time someone sends a message?

I'm not sure if "getElementsWithinColShape" even works if a player doesn't actually enter the colshape, but is already in it when it is created.

And all of this is happening serverside.

Just send the messages to all players and let the clients do the math if they are near enough.

Link to comment
50 is the radius...
A colshape every time someone sends a message?

@Bonsai, have a look at line 10:

destroyElement( chatSphere ) 

Even if the chatsphere is created a billion times, that doesn't matter.

If it wasn't destroyed it would be 100 times worse.

Just because MTA provides a function that lets you get all elements within a colshape, it doesn't mean its faster.

How does it work internally? Maybe after all its just comparing positions aswell, so you could just compare them yourself without having to create and destroy elements, which might also have to be synced with the clients.

Link to comment

what would be the most efficient way of doing this, and how would i do it? seems colshape isnt too effective. my servers pretty loaded as of now, dont really need extra lag. ive seen most RP servers jut have a simple T = local, Y = team, and G,B, or U for global. i just wanted the local for T, but i wanted it to have the same characteristics as freeroam chat, in the sense where it contains player colors and staff tags

Link to comment

Have to admit, Bonsai was right.

It's faster to use getElementsByType and getDistanceBetweenPoints3D instead of using a colshape.

Tested it on my server, the getDistanceBetweenPoints3D way was like 3-4x faster, needed 1ms to use it on 500 peds.

But in my eyes it's still much better than to trigger it to every player.

So you should better use getElementsByType and getDistanceBetweenPoints3D.

local text = "Hi" 
local x, y, z = getElementPosition ( player ) 
local players = getElementsByType ( "player" ) 
for i=1, #players do   -- fastest loop 
    local xp, yp, zp = getElementPosition ( players[i] )  
    if getDistanceBetweenPoints3D ( x, y, z, xp, yp, zp ) <= 50 then 
        outputChatBox ( players[i], text, 200, 200, 200 ) 
    end 
end 

Link to comment
Have to admit, Bonsai was right.

It's faster to use getElementsByType and getDistanceBetweenPoints3D instead of using a colshape.

Tested it on my server, the getDistanceBetweenPoints3D way was like 3-4x faster, needed 1ms to use it on 500 peds.

But in my eyes it's still much better than to trigger it to every player.

So you should better use getElementsByType and getDistanceBetweenPoints3D.

local text = "Hi" 
local x, y, z = getElementPosition ( player ) 
local players = getElementsByType ( "player" ) 
for i=1, #players do   -- fastest loop 
    local xp, yp, zp = getElementPosition ( players[i] )  
    if getDistanceBetweenPoints3D ( x, y, z, xp, yp, zp ) <= 50 then 
        outputChatBox ( players[i], text, 200, 200, 200 ) 
    end 
end 

not sure i understand, this script you posted what is that? just whatever goes before my tag script?

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