How would one about making a local chat (text chat) that would be limited to 30m and then a person could do /g for global chat.
You would need several pieces of data
- Talker, the one that talks in chat
- Range, the range of the chat messages
- Players: the players on the server.
The latter is probably the hardest to obtain, but should be do-able
Some (pseudo) code:
local range = 30
local players = players
local talker = talker
for i = 1, players do
local distance = magnitude(players[i].pos - talker.pos)
if(distance < range)
TriggerServerEvent("propagateChatMessage", GetPlayerServerId(player[i]), chatMessage)
end
end
function magnitude(x, y, z)
// I would assume CFX has a native to calculate the vector magnitude.
return sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2))
end
Note that you can also take the square magnitude and square range to check if it is in range. Square rooting can get kinda heavy on the cpu if you do it every frame, for every player.
On the serverside you would have a event handler for propagateChatMessage
, sending the message to the specific client. You could only invoke propagateChatMessage
on the client only and let the server worry about finding who’s in range (this would be a better approach I think).
Since you would not have to build a client list for every client.
SERVER
AddEventHandler('chatMessage', function(source, message)
if string.sub(message, 1, string.len("/")) ~= "/" then
TriggerClientEvent("sendProximityMessage", -1, source, message)
end
CancelEvent()
end)
CLIENT
RegisterNetEvent('sendProximityMessage')
AddEventHandler('sendProximityMessage', function(id, message)
local myId = PlayerId()
local pid = GetPlayerFromServerId(id)
if pid == myId then
TriggerEvent('chatMessage', "^4" .. name .. "", {0, 153, 204}, "^7 " .. message)
elseif GetDistanceBetweenCoords(GetEntityCoords(GetPlayerPed(myId)), GetEntityCoords(GetPlayerPed(pid)), true) < 19.999 then
TriggerEvent('chatMessage', "^4" .. name .. "", {0, 153, 204}, "^7 " .. message)
end
end)
SERVER
TriggerEvent('es:addCommand', 'G', function(source, args, user)
TriggerClientEvent('chatMessage', -1, "^0[^4GLOBAL^0] (^4@" .. source .. "^0)", {30, 144, 255}, table.concat(args, " "))
end)
HOPE THIS HELP
Try this and tell me if it works, I am not able to test it rn.
Seems like it doesn’t work, I removed the link.