All players receiving message

Hello,

So I have two chat commands, when they’re entered they open a panel. However, when a player types one of the chat commands, I want it to print a message in ALL players chats. I’ve tried the net and usermessages but they haven’t worked for me. I’m still learning so any help would be awesome! Thanks

[lua]

if SERVER then
local chatCommands = {
“!forums”,
“!website”,
}
hook.Add( “PlayerSay”, “ChatCheck”, function( ply, text, bteam )
if ( table.HasValue( chatCommands, text:lower() ) ) then
umsg.Start( “forumsOpen”, ply )
umsg.End()
return “”
end
end )
end
[/lua]

That’s the file in it’s current state without the stuff I’ve tried.

Thanks.

You could send another usermessage but this time to all players, then use chat.AddText( … ) in the usermessage hook in the client realm.

Could you provide an example? I’m still learning, someone helped me make the chat command.

If you remove



return "" 


Then the command they type in chat will show up as normal, like ‘Niandra: !forums’, which might be easier/nicer

Alternatively, you can just k, v in pairs to loop through everyone and use ChatPrint

Ewww no, use net messages, you could also do what Niandra said and chat print, but if you want sexy colors and what not then use my method and alter the addtext.
[lua]

if SERVER then

util.AddNetworkString("ChatOpenUI")
util.AddNetworkString("ChatBroadcast")

local chatCommands = {"!forums","!website"}

hook.Add( "PlayerSay", "ChatCheck", function( ply, text, bteam ) 
	if ( table.HasValue( chatCommands, text:lower() ) ) then 
	
		net.Start("ChatOpenUI")
		net.Send( ply )
		
		net.Start("ChatBroadcast")
		net.WriteString("My message")
		net.Broadcast()
		
		return "" 
	end
end )

else

net.Receive("ChatOpenUI", function()
	-- Create VGUI Here.
end)

net.Receive("ChatBroadcast",function()
	chat.AddText( net.ReadString() )
end)

end

[/lua]

Alright.

Here’s one way to do it:



if ( SERVER ) then

    local chatCommands = {

        "!forums",  
        "!website"

    }

    hook.Add( "PlayerSay", "ChatCheck", function( ply, text, bteam ) 

        if ( table.HasValue( chatCommands, text:lower() ) ) then 

            umsg.Start( "forumsOpen", ply )
            umsg.End()

            umsg.Start( "forumsBroadcast" ) -- If you don't provide a player entity, it will broadcast to all
            umsg.End()

            return "" 

        end

    end )

end

if ( CLIENT ) then

    usermessage.Hook( "forumsBroadcast", function( Buffer )

        chat.AddText( Color( 255, 255, 255 ), "Message here" )

    end )

end


Instead, you could also disregard the creation of the new usermessage and instead use the global function BroadcastLua which has a limit of 254 characters. It would look something like this:



BroadcastLua( "chat.AddText( Color( 255, 255, 255 ), 'Message here' )" )


@McDunkable

Is it possible to have it print the username of the player that used the command?

Thanks for all the suggestions everyone. McDunkables one is easiest for me to understand for the time being.

Yes, although I’m not a fan of the usermessage, I don’t see a reason as to not use it for simple stuff, but it’d be better if you started getting accustomed to the net library. Anyways:

You can network the player entity by calling umsg.Entity( ply ) after umsg.Start and before umsg.End
It would look something like:



umsg.Start( "forumsBroadcast" )
umsg.Entity( ply )
umsg.End()


You can then read the entity in the usermessage hook by calling Buffer:ReadEntity() and storing it in a local variable. For example:



usermessage.Hook( "forumsBroadcast", function( Buffer )

    local Player = Buffer:ReadEntity()

    if ( not ( IsValid( Player ) ) ) then -- Always check if it's a valid entity in the first place.

        return

    end

    chat.AddText( Color( 255, 255, 255 ), Player:Name() .. " opened the forums!" )

end )


Thank you for everyones suggestions! I’ll take the time to read/learn the other two when I have more time.