[Sandbox] Basic chat system

So I’m running a sandbox based RP server which is turning out great (assuming it remains private) but with 5-10 players all roleplaying around the map you can imagine what the chat spam would be like right? What I’m requesting is a simple chat system that is area based (like gamemodes such as Tacoscript and DarkRP which includes a /ooc command and maybe a whisper and /yell command.

Any kind of advice/example or even a full script would be very appreciated, thanks in advance.

This should work fine:
[lua]if (SERVER) then
– A function to add chat in a radius.
function AddChatInRadius(ply, text, radius, prefix)
local pos = ply:GetPos()
local players = ents.FindInSphere(pos, radius)

	if radius == 0 then
		players = player.GetAll()
	end
	
	for k,v in pairs(players) do
		if v:IsPlayer() and ValidEntity(v) then
			umsg.Start("SimpleChat.AddMessage", v)
				umsg.String(ply:GetName())
				umsg.String(text)
				umsg.String(prefix or "")
			umsg.End()
		end
	end
end

-- The hook to check for possible RP chat.
hook.Add("PlayerSay", "SimpleChatPlayerSay", function(ply, text)
	if string.sub(text, 1, 2) == "//" then
		AddChatInRadius(ply, string.sub(text, 3), 0, "(OOC)")
	elseif string.sub(text, 1, 2) == "/w" then
		AddChatInRadius(ply, string.sub(text, 3), 128, "(Whisper)")
	elseif string.sub(text, 1, 2) == "/y" then
		AddChatInRadius(ply, string.sub(text, 3), 512, "(Yell)")
	else
		AddChatInRadius(ply, text, 256)
	end
	
	return ""
end)

else
– Hook into recieving a custom message from the server.
usermessage.Hook(“SimpleChat.AddMessage”, function(msg)
local name = msg:ReadString()
local text = msg:ReadString()
local prefix = msg:ReadString()

	if prefix and prefix != "" then
		chat.AddText(Color(240, 180, 50), prefix.." ", Color(240,240,240), name..":"..text)
	else
		chat.AddText(Color(240, 240, 240), name..": "..text)
	end
	chat.PlaySound()
end)

end
[/lua]
It should make it so that normal chat (no command) creates text in a radius of 256 units.
By putting “//” at the start of the message, the text will be OOC and global.
“/y” will yell the message, creating text in a radius of 512 units.
And finally, “/w” will whisper the message, sending it to clients within 128 units.

However, it is untested in an online environment.

You need to put this in a .lua file and place the .lua file into your servers “lua/autorun” folder.