Chat Commands


function GM:PlayerSay( ply, bTeam, bDead, text )
	
	if ( string.sub( text, 1, 5 ) == "/nick" ) then 
		ply:SetNWString( "nickname", string.sub( text, 5, 25 ) )
		notification.AddLegacy( ply:GetName().. " Has changed there name to " ..string.sub( text, 5, 25), NOTIFY_HINT, 3 )
		return ""
		else
	end

	if ( ply:GetNWString( "nickname" ) != nil and string.sub( text ) ) then
	for k,v in pairs(player.GetAll()) do
	   chat.AddText( ply:GetNWString( "nickname" ), Color( 0, 255, 255 ), ":", Color( 255, 255, 255 ), string.sub( text ) )
	end
	   return false
	   else
	end
	
end

Ok, so this is mean’t to when a player types /nick it sets there name and it should work! but console: `

[ERROR] gamemodes/gmtower2/gamemode/init.lua:7: bad argument #1 to ‘sub’ (string expected, got boolean)

  1. sub - [C]:-1
  2. unknown - gamemodes/gmtower2/gamemode/init.lua:7

Check the wiki:

GM:PlayerSay
You have your arguments in the wrong order. It should be:



function GM:PlayerSay( ply, text, bTeam )


[editline]14th August 2016[/editline]

Also, since you’re using chat.AddText you should really be using

GM:OnPlayerChat since that’s the clientside version

[editline]14th August 2016[/editline]

Also, this won’t work:



for k,v in pairs(player.GetAll()) do
    chat.AddText( ply:GetNWString( "nickname" ), Color( 0, 255, 255 ), ":", Color( 255, 255, 255 ), string.sub( text ) )
end


That doesn’t make chat.AddText display for every player, it just adds chat.AddText to the local player’s chat a ton of times - the amount of times is the amount of players

[editline]14th August 2016[/editline]

Also
Has changed there name to
Should be
Has changed their name to

[editline]14th August 2016[/editline]

Also, this thing:



if ( ply:GetNWString( "nickname" ) != nil and string.sub( text ) ) then
    for k,v in pairs(player.GetAll()) do
        chat.AddText( ply:GetNWString( "nickname" ), Color( 0, 255, 255 ), ":", Color( 255, 255, 255 ), string.sub( text ) )
    end
    return false
    else -- This isn't needed
end


Is quite broken because not only will it not add chat text to every player’s chatbox, but if the player team chatted, then every player would see what they chatted

[editline]14th August 2016[/editline]

Also,

notification.AddLegacy is a clientside function and you’re calling it in a serverside thing.

Oh ok, but how may I change the players name? without using SetName because for some reason it dont work…

Can’t you just add a

GM:OnPlayerChat function and in that modify the name outputted? I’m working on an example, hold on


function GM:OnPlayerChat( ply, text, bTeam )
	
	if ( string.sub( text, 1, 5 ) == "/nick" ) then 
		ply:SetNWString( "nickname", string.sub( text, 6, 25 ) )
		notification.AddLegacy( ply:GetName().. " Has changed their name to " ..string.sub( text, 6, 25), NOTIFY_HINT, 3 )
		return ""
	end

	if ( ply:GetNWString( "nickname" ) != nil ) then
	   chat.AddText( ply:GetNWString( "nickname" ), Color( 0, 255, 255 ), " : ", Color( 255, 255, 255 ), string.sub( text, 1, 100 ) )
	end
	
end

What I have so far…

[editline]14th August 2016[/editline]

But I wan’t to stop the players original name from displaying… ?

Try this:



-- serverside

hook.Add( 'PlayerSay', 'blah', function( ply, txt, bTeam )
	
	if ( string.sub( txt, 1, 5 ) == "/nick" ) then
	
		local newname = string.sub( txt, 7, 25 )
		ply:SetNWString( "nickname", newname )

		PrintMessage( HUD_PRINTTALK, ply:Nick().. " has changed their name to " ..newname )
		
		return ""
		
	end
	
end )

-- clientside

hook.Add( 'OnPlayerChat', 'blah2', function( ply, txt, bTeam, bDead )

	local name = ply:GetNWString( 'nickname', ply:Nick() ) -- ply:Nick will be used if nickname isn't valid
	
	chat.AddText( name, Color( 255, 255, 255 ), " : ", txt )
	
	return true

end )


Works fine for me

[editline]14th August 2016[/editline]

By the way,

Entity:SetNWString doesn’t work in the OnPlayerChat hook since as the wiki says, if it’s run clientside (like OnPlayerChat is) then the network string will only be set clientside- meaning that any other player that needs to see the player’s custom name will not be able to as the network string doesn’t exist for them

Works! Is there a way I can change the tab name aswell?

[editline]14th August 2016[/editline]

It is a custom tab*

[editline]14th August 2016[/editline]

And will it show everyplayer? my chat message?

[editline]14th August 2016[/editline]

Is there a way to test if its more then 4 letters (the nick) ? like I don’t want players with names with nothing like : Hey Jacob… and you cant see there name… if they type /nick

Use the length operator (#), or in special cases string.len



print(#str == string.len(str))


I say special because for the most part the operator is faster and usually you have no reason not to use it.