OnPlayerChat | Tags

I have been working on my starwars rp server (from scratch) and I have come across something I never thought of doing before and I really look forward to doing this; color chat with regiment and class tags (just networked strings) but in the players name when he or her talk in chat.

How might I do this, I have looked up how and all I can find is OnPlayerChat, apon editing and bug testing I had no luck except errors.
Any ideas?


-- Ranking names in order, the rank that comes first is always used for new players!
GM.Config.Ranks = { "Cadet", "PVT", "PFC", "LCPL","CPL", "SGT", "CSGT", "SGM", "COM" }

-- Default Regiments in order (You can add new regiments ingame)
GM.Config.DefaultRegiments = { 
	{
		name = "Recruit", 
		color = Color( 200, 200, 200 ), 
		weapons = { "weapon_fists" },
		model = "",
		id = 1
	},
	{
		name = "Storm Trooper", 
		color = Color( 200, 10, 200 ), 
		weapons = { "weapon_fists" },
		model = "",
		id = 2
	}
}


player:GetNWString( “RPRank” ) and player:GetNWInt( “Regiment” ) – Returns the regiments number id.

Can someone help?

What have you tried so far with OnPlayerChat hook?



function GM:OnPlayerChat( player, strText, bTeamOnly, bPlayerIsDead )

	--
	-- I've made this all look more complicated than it is. Here's the easy version
	--
	-- chat.AddText( player, Color( 255, 255, 255 ), ": ", strText )
	--

	local tab = {}

	if ( bPlayerIsDead ) then
		table.insert( tab, Color( 255, 30, 40 ) )
		table.insert( tab, "*DEAD* " )
	end

	if ( bTeamOnly ) then
		table.insert( tab, Color( 30, 160, 40 ) )
		table.insert( tab, "( TEAM ) " )
	end

	if ( IsValid( player ) ) then
                table.insert( tab, player:GetNWString("RPRank") .." ".. player )
	else
		table.insert( tab, "Console" )
	end

	table.insert( tab, Color( 255, 255, 255 ) )
	table.insert( tab, ": "..strText )

	chat.AddText( unpack( tab ) )

	return true

end


And I tried looping my configuration of my default regiments to see if the player is that regiment id in ply:GetNWInt(“Regiment”) <-- example … and if they are in regiment then grab reg[“name”] and reg[“color”] so it looks like this: [Regiment Color] [Regiment Name] [Rank] [Name]: [Message]

But when I tried it I got errors and nothing happen so I shortened it to the rank thingo

[editline]10th October 2017[/editline]

(and yes the good coder I am I copied and pasted it to just see how it worked)

The problem with your code is this line:
[lua]table.insert( tab, player:GetNWString(“RPRank”) …" "… player )[/lua]
In this line, you’re attempting to concatenate a string with a player, which you can’t do. If you take a look at

chat.AddText, you can see that it accepts a Player as an argument which results in the name of the player being added. But when you try to concatenate a string with a userdata value, it’ll error. So what you’ll want to do instead is insert the “RPRank” into the table before the Player - and make sure you always have a fallback when using

Entity:GetNWString.

so can I just do player:Nick()?

Yup, that will return his username. If you want to concatenate vectors or numbers you can do tostring(number)

Okay ill try it I guess thnx (I already knew about tostring() lmao)

[editline]10th October 2017[/editline]


include("config.lua")
include("functions.lua")

if CLIENT then
	function GM:OnPlayerChat( player, strText, bTeamOnly, bPlayerIsDead )
		local tab = {}

		if ( bPlayerIsDead ) then
			table.insert( tab, Color( 255, 30, 40 ) )
			table.insert( tab, "*DEAD* " )
		end

		if ( bTeamOnly ) then
			
			table.insert( tab, Color( 100,100,100 ) )
			table.insert( tab, "( TEAM ) " )
		end

		if ( IsValid( player ) ) then
			table.insert( tab, Color( 255, 255, 90 ) )
			table.insert( tab, (player:GetNWString("RPRank") or "CPT") .." ".. (GetRegiment(player) or "Unknown") .." ".. player:Nick() )
		else
			table.insert( tab, "Console" )
		end

		table.insert( tab, Color( 255, 255, 255 ) )
		table.insert( tab, ": "..strText )

		chat.AddText( unpack( tab ) )

		return true

	end

end

-- Functions.lua --
include("config.lua")

function GetRegiment(ply)
	for k,v in pairs( GM.Config.DefaultRegiments ) do
		if IsValid(v["id"]) then
			if ply:GetNWInt( "Regiment" ) == v["id"] then
				return tostring(v["name"])
			end
		end
	end

	return "Unknown"
end

Now this still doesnt work, in my init I have this…


AddCSLuaFile()
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("functions.lua")
AddCSLuaFile("levels.lua")
AddCSLuaFile("config.lua")

include("cl_init.lua")
include("levels.lua")
include("functions.lua")
include("config.lua")

function GM:PlayerInitialSpawn( ply )

	if ply:GetNWInt( "Regiment" ) == (nil or 0) then
		ply:SetNWInt( "Regiment", GM.Config.StartingRegiment )
	end

	if ply:GetNWString( "RPRank" ) == (nil or "") then
		ply:SetNWString( "RPRank", tostring(GM.Config.Ranks[1]) )
	end

end

PrintTable( GM.Config.DefaultRegiments )

Now for some stupid reason… it really doesn’t want to work…
I have tried using this function and just simply modified the colors … it doesn’t want to work? do I have to disable something>

Is this your own gamemode? You should not be overwriting the Gamemode hook if not.

What Nookyava is saying is, instead of doing


function GM:OnPlayerChat
end

use hook.Add because if you use the function, it overrides what the gamemode wants it to do and makes it execute that code everytime a player chats.

It seems like it is, as he says he’s making his roleplay from scratch, unless he’s completely misusing that term

Nowadays it’s pretty tough to say since when some people say that, they mean they’re using DarkRP as the base.

I am using Sandbox as a base, would I have to use a hook to ovveride the base function it uses?

[editline]11th October 2017[/editline]

Ontop of that currently I have not added the derive gamemode function so its currently o base

You’ve made no sense.

You’re using Sandbox as a base but you’re not deriving the gamemode at all, so it’s using no base?..

What?

Maybe my problem is I havent derived any gamemode lmao