Jump to content

Simple "LocalChat" Question


Raffica

Recommended Posts

Hey there! I just started with lua scripting so I'm a newbie. After an hour of "googling" I managed to make this what people call localchat. But I got one (if not more ;P) problem, when I type in localchat the text is in the same color as the players name and I don't want this. I want the text to be white but keep the players color. I added #ffffff at line 26 but when the player is not in team the chat get like this: Raffica: #ffffff Hello. Which is anoying. Please help me with this!

Script:

chat_range=75  
addEventHandler("onPlayerJoin",getRootElement(), 
function () 
bindKey(source,"u","down","chatbox","NearbyChat") 
end) 
  
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()), 
function () 
for index, player in pairs(getElementsByType("player")) do 
bindKey(player,"u","down","chatbox","NearbyChat") 
  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 msg = table.concat({...}, " ") 
  local nick=getPlayerName(player) 
local r,g,b = getTeamColor(getPlayerTeam(player)) 
  for _,v in ipairs(getElementsByType("player")) do 
    if isPlayerInRangeOfPoint(v,px,py,pz,chat_range) then 
      outputChatBox("(Nearby)"..nick..": #ffffff"..msg,v,r,g,b,true) 
    end 
  end 
end 
addCommandHandler("NearbyChat",onChat) 

Link to comment

try this :

EDIT

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 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..": #ffffff"..msg,v,r,g,b,true) 
        end 
    end 
end 
addCommandHandler("Local",onChat) 

Link to comment

Usually when it outputs like that its because the element is incorrect!

  for _,v in ipairs(getElementsByType("player")) do 
    if isPlayerInRangeOfPoint(v,px,py,pz,chat_range) then 
      outputChatBox("(Nearby)"..nick..": #ffffff"..msg,v,r,g,b,true) 
    end 
  end 

I think it should be

  for _,v in ipairs(getElementsByType("player")) do 
    if isPlayerInRangeOfPoint(v,px,py,pz,chat_range) then 
      outputChatBox("(Nearby)"..nick..": #ffffff"..msg,getPlayerName(v),r,g,b,true) 
    end 
  end 

Link to comment
try this :

EDIT

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 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..": #ffffff"..msg,v,r,g,b,true) 
        end 
    end 
end 
addCommandHandler("Local",onChat) 

But now it's the same color all the time.. I want the player name to be same color as their team and if no team players name should be white.

Link to comment
Usually when it outputs like that its because the element is incorrect!
  for _,v in ipairs(getElementsByType("player")) do 
    if isPlayerInRangeOfPoint(v,px,py,pz,chat_range) then 
      outputChatBox("(Nearby)"..nick..": #ffffff"..msg,v,r,g,b,true) 
    end 
  end 

I think it should be

  for _,v in ipairs(getElementsByType("player")) do 
    if isPlayerInRangeOfPoint(v,px,py,pz,chat_range) then 
      outputChatBox("(Nearby)"..nick..": #ffffff"..msg,getPlayerName(v),r,g,b,true) 
    end 
  end 

Sorry didn't solve the problem :?

Link to comment

Btw , why nobody saw that the resource is stolen ?

After an hour of "googling" I managed to make this what people call localchat

This line means that he made it.. but the script is aleardy posted here : https://community.multitheftauto.com/index.php?p= ... ls&id=2801

That script

  
chat_range=100 
  
addEventHandler("onPlayerJoin",getRootElement(), 
function () 
bindKey(source,"u","down","chatbox","LocalChat") 
end) 
  
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()), 
function () 
for index, player in pairs(getElementsByType("player")) do 
bindKey(player,"u","down","chatbox","LocalChat") 
  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 msg = table.concat({...}, " ") 
  local nick=getPlayerName(player) 
local r,g,b = getTeamColor(getPlayerTeam(player)) 
  for _,v in ipairs(getElementsByType("player")) do 
    if isPlayerInRangeOfPoint(v,px,py,pz,chat_range) then 
      outputChatBox("(LocalChat)"..nick..": #ffffff"..msg,v,r,g,b,true) 
    end 
  end 
end 
addCommandHandler("LocalChat",onChat) 
  

His script :

  
chat_range=75  
addEventHandler("onPlayerJoin",getRootElement(), 
function () 
bindKey(source,"u","down","chatbox","NearbyChat") 
end) 
  
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()), 
function () 
for index, player in pairs(getElementsByType("player")) do 
bindKey(player,"u","down","chatbox","NearbyChat") 
  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 msg = table.concat({...}, " ") 
  local nick=getPlayerName(player) 
local r,g,b = getTeamColor(getPlayerTeam(player)) 
  for _,v in ipairs(getElementsByType("player")) do 
    if isPlayerInRangeOfPoint(v,px,py,pz,chat_range) then 
      outputChatBox("(Nearby)"..nick..": #ffffff"..msg,v,r,g,b,true) 
    end 
  end 
end 
addCommandHandler("NearbyChat",onChat) 

Not many differences , huh ?

Link to comment

Did I say that I CREATED this? No.. I told you I am just trying to learn lua. I am not a scripter.. Please if you are going to accuse me for "stealing" scripts to learn, then don't post here because I made this topic to get an answer that will help me to the next step in my learning. And if you noticed this now you must be blind because I clearly wrote that I "googled" to find this. And btw I am not making a server so why would I want to steal stuff? Now please go on topic and help me or just leave me alone..

EDIT: Sorry for using the word "make" >.>

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