[HELP] The /looc command!

Hello everyone!

First of all, sorry for the potential mistakes and sorry for posting this thread. I am a beginner at lua coding and I only know the very basics. I have decided to come here after a TON of research on the internet to find a solution to my problem. Sadly I didn’t find one.

What I am trying to do:

I am trying to make it so any player on my server can type “/looc [Message]” and then that message will appear in chat to anyone around them.
This is going to be used as a local out of character chat for my players.

I also need the command to either be in a different color then white or have some sort of identifier before the text, such as “[LOOC] John Doe that’s against the rules!”

Can you please tell me what is the script or command to make this thing for my server?
I really need your help on this, i’ve been searching for a while, thanks for your help!

Also, network the text and player clientside to every player found within the given area and use

chat.AddText to make the text show up clientside.

Okay, this is what I have done so far. I’m sort of confused so… what should I do too fix this?
function loocCommand( ply, text, public )
if( string.sub( text, 1, 5 ) == “!looc” ) then
pl:ConCommand( “looc” )
chat.AddText( Color( 255, 255, 255 ), ply, “, LOOC:”)
ents.FindInSphere( 0, 10 )
end
end
hook.Add( “PlayerSay”, “loocCommand”, myCommand )

PlayerSay is a serverside hook. chat.AddText is a clientside function. You need to network it, or move the data from the server to the client.



if SERVER then
	util.AddNetworkString('LOOC') -- Whenever you are networking, you need to add the string like so or you will get an error.
	hook.Add('OnPlayerChat', 'LOOC', function(ply, text, public)
		if text:sub(1,5) == '!looc' then
			for _, e in pairs(ents.FindInSphere(ply:GetPos(), 50)) do -- Here we iterate through all of the entities within 50 units of the player.
				if e:IsPlayer() then -- This detects all entities, so make sure the entity is a player.
					net.Start('LOOC')
						net.WriteEntity(ply) -- Write the sender
						net.WriteString(text) -- Write the text
					net.Send(e) -- Send it to the player.
				end
			end
		return '' -- Make sure that the actual message does not appear.
		end
	end)
else

	net.Receive('LOOC', function(len)
		local ply = net.ReadEntity() -- Read the player and text clientside.
		local text = net.ReadString()
		chat.AddText(Color(0,255,0), '[LOOC] ', team.GetColor(ply:Team()), ply:Name(), Color(255,255,255), ': '..text  ) -- Use the chat.AddText format to display the LOOC tag, the player's name, and the text.
	end)
end


http://wiki.garrysmod.com/page/chat/AddText

http://wiki.garrysmod.com/page/Net_Library_Usage

Untested, but it should show you the concept.

Okay. Im starting to get it. So if I pasted this into a looc.lua file. Would this work if I put the file in a specific location? Also what location would that be?

Since this has both clientside and serverside code, you need to run it in the shared realm, which is basically on all of the clients and the server. Put it in lua/autorun to run it shared.

I put it in lua/autorun and nothing happens, when I type !looc in chat.

Sorry, I made a mistake because I was in a rush. Just tested this and it works fine.



if SERVER then
	util.AddNetworkString('LOOC') -- Whenever you are networking, you need to add the string like so or you will get an error.
	hook.Add('PlayerSay', 'LOOCcommand', function(ply, text, public)
		if text:sub(1,5) == '!looc' then
			for _, e in pairs(ents.FindInSphere(ply:GetPos(), 50)) do -- Here we iterate through all of the entities within 50 units of the player.
				if e:IsPlayer() then -- This detects all entities, so make sure the entity is a player.
					net.Start('LOOC')
						net.WriteEntity(ply) -- Write the sender
						net.WriteString(text:sub(7)) -- Write the text
					net.Send(e) -- Send it to the player.
				end
			end
		return '' -- Make sure that the actual message does not appear.
		end
	end)
else
	net.Receive('LOOC', function(len)
		local ply = net.ReadEntity() -- Read the player and text clientside.
		local text = net.ReadString()
		chat.AddText(Color(0,255,0), '[LOOC] ', team.GetColor(ply:Team()), ply:Name(), Color(255,255,255), ': '..text  ) -- Use the chat.AddText format to display the LOOC tag, the player's name, and the text.
	end)
end


You may want to call the default chat hook to add some compatibility to other scripts.