Jump to content

[HELP] Local chat in dimension


kowixd

Recommended Posts

Hello have local chat in freeroam server

I am new at this
 

I try to make it look in one dimension.
For example, if I am in dimension 5, only those in dimension 5 can see it

Help please :(

chat_range=100
 
addEventHandler("onPlayerJoin",getRootElement(),
function ()
bindKey(source,"u","down","chatbox","Local")
end)
 
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()),
function ()
for index, player in pairs(getElementsByType("player")) do
bindKey(player,"u","down","chatbox","Local")
  end
end)
 
function isPlayerInRangeOfPoint(player,x,y,z,range)
   local px,py,pz=getElementPosition(player)
   return ((x-px)^2+(y-py)^2+(z-pz)^2)^0.5<=range
end
 
function onChat(player,_,...)
  local px,py,pz=getElementPosition(player)
  local dm = getElementDimension(player)
  local msg = table.concat({...}, " ")
  local nick=getPlayerName(player)
  local r,g,b = getPlayerNametagColor(player)
  for _,v in ipairs(getElementsByType("player")) do
   
    if isPlayerInRangeOfPoint(v,px,py,pz,chat_range)  then 
        outputChatBox("(Local) "..nick..": "..msg,v,r,g,b,true)

    end
    -- outputServerLog("(Local) "..nick..": "..msg,true)
  end
end
addCommandHandler("Local",onChat)

Link to comment
chat_range=100
 
addEventHandler("onPlayerJoin",getRootElement(),
function ()
bindKey(source,"u","down","chatbox","Local")
end)
 
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()),
function ()
for index, player in pairs(getElementsByType("player")) do
bindKey(player,"u","down","chatbox","Local")
  end
end)
 
function isPlayerInRangeOfPoint(player,x,y,z,range)
   local px,py,pz=getElementPosition(player)
   return ((x-px)^2+(y-py)^2+(z-pz)^2)^0.5<=range
end
 
function onChat(player,_,...)
  local px,py,pz=getElementPosition(player)
  local dm = getElementDimension(player)
  local msg = table.concat({...}, " ")
  local nick=getPlayerName(player)
  local r,g,b = getPlayerNametagColor(player)
  for _,v in ipairs(getElementsByType("player")) do
   
    if isPlayerInRangeOfPoint(v,px,py,pz,chat_range)  then 
        outputChatBox("(Local) "..nick..": "..msg,v,r,g,b,true)

    end
    -- outputServerLog("(Local) "..nick..": "..msg,true)
  end
end
addCommandHandler("Local",onChat)

Next time use the this code thing. It's the "<>" button. 

You have to use a for iteration btw, where you get the dimension of the players nearby (or all the players) and make an if that checks if the players' dimension is the same. If it is, only then do the outputChatBox for that player.  getElementDimension return the dimension an element is in. 

Edited by WorthlessCynomys
Link to comment
On 21/3/2018 at 09:29, WorthlessCynomys said:

chat_range=100
 
addEventHandler("onPlayerJoin",getRootElement(),
function ()
bindKey(source,"u","down","chatbox","Local")
end)
 
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()),
function ()
for index, player in pairs(getElementsByType("player")) do
bindKey(player,"u","down","chatbox","Local")
  end
end)
 
function isPlayerInRangeOfPoint(player,x,y,z,range)
   local px,py,pz=getElementPosition(player)
   return ((x-px)^2+(y-py)^2+(z-pz)^2)^0.5<=range
end
 
function onChat(player,_,...)
  local px,py,pz=getElementPosition(player)
  local dm = getElementDimension(player)
  local msg = table.concat({...}, " ")
  local nick=getPlayerName(player)
  local r,g,b = getPlayerNametagColor(player)
  for _,v in ipairs(getElementsByType("player")) do
   
    if isPlayerInRangeOfPoint(v,px,py,pz,chat_range)  then 
        outputChatBox("(Local) "..nick..": "..msg,v,r,g,b,true)

    end
    -- outputServerLog("(Local) "..nick..": "..msg,true)
  end
end
addCommandHandler("Local",onChat)

Next time use the this code thing. It's the "<>" button. 

You have to use a for iteration btw, where you get the dimension of the players nearby (or all the players) and make an if that checks if the players' dimension is the same. If it is, only then do the outputChatBox for that player.  getElementDimension return the dimension an element is in. 

Can you explain me more please?
what do you mean by "for iteration btw"
Sorry, I'm still starting on this :( 

Link to comment
chat_range=100
 
addEventHandler("onPlayerJoin",getRootElement(),
function ()
bindKey(source,"u","down","chatbox","Local")
end)
 
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()),
function ()
for index, player in pairs(getElementsByType("player")) do
bindKey(player,"u","down","chatbox","Local")
  end
end)
 
function isPlayerInRangeOfPoint(player,x,y,z,range)
   local px,py,pz=getElementPosition(player)
   return ((x-px)^2+(y-py)^2+(z-pz)^2)^0.5<=range
end
 
function onChat(player,_,...)
  local px,py,pz=getElementPosition(player)
  local dm = getElementDimension(player)
  local msg = table.concat({...}, " ")
  local nick=getPlayerName(player)
  local r,g,b = getPlayerNametagColor(player)
  for _,v in ipairs(getElementsByType("player")) do
    if ( getElementDimension ( v ) == 5 ) then 
    if isPlayerInRangeOfPoint(v,px,py,pz,chat_range) then 
        outputChatBox("(Local) "..nick..": "..msg,v,r,g,b,true)
end 
    end
    -- outputServerLog("(Local) "..nick..": "..msg,true)
  end
end
addCommandHandler("Local",onChat)

 

Link to comment
  • Discord Moderators
--> client
bindKey("b", "down", "chatbox", "Local")
--> server
local CHAT_RANGE = 100--> meters

local function localChat(source, _, ...)
    local dm = getElementDimension(source)
  	local msg = "(Local) "..(getPlayerName(source) or "Unknown")..": "..table.concat({...}, " ")
    local r, g, b = getPlayerNametagColor(source)
    local sourceDim = getElementDimension(source)
    local sourcePos = Vector3(getElementPosition(source))

    for _, player in pairs(getElementsByType("player")) do
        if (getElementDimension(player)==sourceDim) then
            if (getDistanceBetweenPoints3D(sourcePos, getElementPosition(player))<=CHAT_RANGE) then
                --If hes in the same dimension as the source[player]
                outputChatBox(msg, player, r, g, b, true)--> It will output the msg to chatbox with colorcodes[So, if he types #10FFFF, then it will be a redish text.]. Set 'true' to 'false' to disable this.
            end
        end
    end
end
addCommandHandler("local", localChat, false, false)
addCommandHandler("Local", localChat)

 

Edited by Pirulax
  • Like 1
Link to comment
13 hours ago, DABL said:

chat_range=100
 
addEventHandler("onPlayerJoin",getRootElement(),
function ()
bindKey(source,"u","down","chatbox","Local")
end)
 
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()),
function ()
for index, player in pairs(getElementsByType("player")) do
bindKey(player,"u","down","chatbox","Local")
  end
end)
 
function isPlayerInRangeOfPoint(player,x,y,z,range)
   local px,py,pz=getElementPosition(player)
   return ((x-px)^2+(y-py)^2+(z-pz)^2)^0.5<=range
end
 
function onChat(player,_,...)
  local px,py,pz=getElementPosition(player)
  local dm = getElementDimension(player)
  local msg = table.concat({...}, " ")
  local nick=getPlayerName(player)
  local r,g,b = getPlayerNametagColor(player)
  for _,v in ipairs(getElementsByType("player")) do
    if ( getElementDimension ( v ) == 5 ) then 
    if isPlayerInRangeOfPoint(v,px,py,pz,chat_range) then 
        outputChatBox("(Local) "..nick..": "..msg,v,r,g,b,true)
end 
    end
    -- outputServerLog("(Local) "..nick..": "..msg,true)
  end
end
addCommandHandler("Local",onChat)

 

thanks but, it only makes it look in the dimension 5

4 hours ago, Pirulax said:

--> client
bindKey("b", "down", "chatbox", "Local")
--> server
local CHAT_RANGE = 100--> meters

local function localChat(source, _, ...)
    local dm = getElementDimension(source)
  	local msg = "(Local) "..(getPlayerName(source) or "Unknown")..": "..table.concat({...}, " ")
    local r, g, b = getPlayerNametagColor(source)
    local sourceDim = getElementDimension(source)
    local sourcePos = Vector3(getElementPosition(source))

    for _, player in pairs(getElementsByType("player")) do
        if (getElementDimension(player)==sourceDim) then
            if (getDistanceBetweenPoints3D(sourcePos, getElementPosition(player))<=CHAT_RANGE) then
                --If hes in the same dimension as the source[player]
                outputChatBox(msg, player, r, g, b, true)--> It will output the msg to chatbox with colorcodes[So, if he types #10FFFF, then it will be a redish text.]. Set 'true' to 'false' to disable this.
            end
        end
    end
end
addCommandHandler("local", localChat, false, false)
addCommandHandler("Local", localChat)

 

It works, thank you!!
:D:D 

Link to comment
  • Discord Moderators
local CHAT_RANGE = 100--> meters

local function localChat(source, _, ...)
  	if not (...) then return end
    local dm = getElementDimension(source)
  	local msg = "(Local) "..(getPlayerName(source) or "Unknown")..": "..table.concat({...}, " ")
    local r, g, b = getPlayerNametagColor(source)
    local sourceDim = getElementDimension(source)
    local sourcePos = Vector3(getElementPosition(source))

    for _, player in pairs(getElementsByType("player")) do
        if (getElementDimension(player)==sourceDim) then
            if (getDistanceBetweenPoints3D(sourcePos, getElementPosition(player))<=CHAT_RANGE) then
                --If hes in the same dimension as the source[player]
                outputChatBox(msg, player, r, g, b, true)--> It will output the msg to chatbox with colorcodes[So, if he types #10FFFF, then it will be a redish text.]. Set 'true' to 'false' to disable this.
            end
        end
    end
end
addCommandHandler("local", localChat, false, false)
addCommandHandler("Local", localChat)

@kowixd Use this instead please, i've forgot something.

  • Like 1
Link to comment
5 hours ago, Pirulax said:

local CHAT_RANGE = 100--> meters

local function localChat(source, _, ...)
  	if not (...) then return end
    local dm = getElementDimension(source)
  	local msg = "(Local) "..(getPlayerName(source) or "Unknown")..": "..table.concat({...}, " ")
    local r, g, b = getPlayerNametagColor(source)
    local sourceDim = getElementDimension(source)
    local sourcePos = Vector3(getElementPosition(source))

    for _, player in pairs(getElementsByType("player")) do
        if (getElementDimension(player)==sourceDim) then
            if (getDistanceBetweenPoints3D(sourcePos, getElementPosition(player))<=CHAT_RANGE) then
                --If hes in the same dimension as the source[player]
                outputChatBox(msg, player, r, g, b, true)--> It will output the msg to chatbox with colorcodes[So, if he types #10FFFF, then it will be a redish text.]. Set 'true' to 'false' to disable this.
            end
        end
    end
end
addCommandHandler("local", localChat, false, false)
addCommandHandler("Local", localChat)

@kowixd Use this instead please, i've forgot something.

ok, thanks :D

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