chat.addtext help, how to send the player different elements?!?

hey guys so i just want the number shown ( EX. == 350) to pop up in a player’s/killer’s chat. I have no idea how to input it and was hoping someone would. Thanks in advanced.


	local class = {}
	class["npc_zombie"] = 10
	class["npc_fastzombie"] = 10
	class["npc_fastzombie_torso"] = 4
	class["npc_headcrab_poison"] = 12
	class["npc_zombie_torso"] = 3
	class["npc_zombine"] = 15
	class["npc_headcrab"] = 6
	class["npc_headcrab_fast"] = 5
	class["npc_headcrab_black"] = 9
	class["npc_antlion"] = 7
	class["npc_antlionguard"] = 350
	local function NPCKilled( victim, killer, weapon )
		if IsValid(victim) && IsValid(killer) then
			if !killer:IsPlayer() then return end
			if !victim:IsNPC() then return end
			if class[victim:GetClass()] then
				killer:AddXP(class[victim:GetClass()])
				killer:SendLua([[chat.AddText(Color(0, 155, 0), "+10 EXP" .. class)]]) -- Need help here class is NOT correct


What are you trying to do? Your “class” variable is a table, you can’t just concatenate it with a string. Did you mean to use victim:GetClass()? Or are you trying to add 10 + the experience for killing the npc?

Also, be prepared for someone to point out your use of SendLua. You should try using the net library instead.

class is a table so you can’t concatenate it unless you set the __concat meta-table function for table…

To display 350 you’d use class.npc_antlionguard or class[ “npc_antlionguard” ] or class[ victim:GetClass( ) ]

https://dl.dropboxusercontent.com/u/26074909/tutoring/tables/quick_intro_to_tables.lua.html

https://dl.dropboxusercontent.com/u/26074909/tutoring/strings/string_concatenation.lua.html

Thanks for the quick reply. I am trying to get it to show the number of exp you get that matches the string number. Like if you kill a fast headcrab it says +5 EXP in chat. I am not any sort of a somewhat good coder so I might have to stick with the simple send lua.

[editline]20th February 2015[/editline]

How would I set the table in this case?

Read mine, it’ll show you how to set it up. And, as Mista Tea says, SendLua isn’t the best way of doing things. I’d also recommend setting up a default value so if class[ victim:GetClass( ) ] doesn’t exist, it falls back to a default value…

You can also define the table differently, here, I rewrote it to show you how to use net, etc…


//
// SERVER side
//
if ( SERVER ) then
	//
	// Initialization
	//

	// Add the net-messages we need to the pool
	util.AddNetworkString( "ChatMessage" );

	// Add our "npc to xp" table...
	local class = {
		// Enemy Classes and XP to gain...
		npc_zombie				= 10;
		npc_fastzombi			= 10;
		npc_fastzombie_torso	= 4;
		npc_headcrab_poison		= 12;
		npc_zombie_torso		= 3;
		npc_zombin				= 15;
		npc_headcrab			= 6;
		npc_headcrab_fast		= 5;
		npc_headcrab_black		= 9;
		npc_antlion				= 7;
		npc_antlionguard		= 350;

		// Default is our fall-back method..
		default					= 1;
	};


	//
	// With hooks we only want to return non-nil for specific behavior.. You returned nil so you're fine but I condensed it all to 1 line..
	//
	hook.Add( "PlayerDeath", "XPOnNPCDeath", function( victim, killer, _w )
		if ( IsValid( victim ) && IsValid( killer ) && killer:IsPlayer( ) && victim:IsNPC( ) ) then
			// Use the npc class to xp table above for xp if the npc exists, otherwise use the default value...
			local _xp = class[ victim:GetClass( ) ] || class.default;

			// Add XP
			killer:AddXP( _xp );

			// Network it... ( You can also now use this in ANY code you write to network a chat-message )
			net.Start( "ChatMessage" );
				// The actual message is "+<XP GIVEN> EXP Added!"
				net.WriteString( "+" .. _xp .. " EXP Added!" );

				// The color we want the chat-message to be for adding xp...
				net.WriteColor( Color( 0, 155, 0, 255 ) );
			net.Send( killer );
		end
	end );
else
	//
	// This is the CLIENT side
	//


	//
	// Add a networking receiver for ChatMessage...
	//
	net.Receive( "ChatMessage", function( _len )
		// Read the chat text or use "N/A" if none-received... although there should always be one..
		local _text = net.ReadString( ) || "N/A";

		// Read the color, or use a default white color if none received...
		local _color = net.ReadColor( ) || color_white;

		// Now, add it to the client chat-window and play a sound...
		chat.AddText( _color, _text );
		chat.PlaySound( );
	end );
end

EDIT: How do you “SET” the table??? You defined the table just fine, it can be defined other ways. The Intro to Tables link I sent shows several ways to ACCESS values, and you also accessed the table just fine in your if class[ victim:GetClass( ) ] then… statement… You just needed to add class[ victim:GetClass( ) ] to your CHAT message BUT, it’d be better to have a default value to fall back on just in case something happens where the npc class doesn’t exist. You can also simplify logic that way…

I used PlayerDeath but set the hook to whichever hook you use because I can’t recall if PlayerDeath handles NPC death too…

Edit 2: I modified the code just a bit more for more comments and I changed the ChatMessage network receiver and sending code so you send the message first then the color so if a color isn’t sent it isn’t a big deal… ( It could be written differently so you can define ChatMessage or Notify as a function and it’ll automatically network or output it depending on the realm it is used in )

Edit 3: Here’s the alternative code which sets up a new meta-table function for Player:ChatMessage so it automatically networks it and can be used in your game-mode to network data or display depending on the realm it is called in; all without using SendLua… The net.Receive is mandatory on the clientside to call it and then that’s all you need for it to work…


//
// SHARED
//


//
// Shared ChatMessage function which will either display chat message, or network it...
//
//
local META_PLAYER = FindMetaTable( "Player" );
function META_PLAYER:ChatMessage( _text, _color, _nosound )
	if ( SERVER ) then
		// Network it... ( You can also now use this in ANY code you write to network a chat-message )
		net.Start( "ChatMessage" );
			// Network the message
			net.WriteString( _text );

			// Network the color we want the chat-message to be
			net.WriteColor( _color );
		net.Send( self );
	else
		chat.AddText( ( _color || color_white ), ( _text || "N/A" ) );

		// If _nosound is defined, don't play it...
		if ( !_nosound ) then
			chat.PlaySound( );
		end
	end
end


//
// SERVER side
//
if ( SERVER ) then
	//
	// Initialization
	//

	// Add the net-messages we need to the pool
	util.AddNetworkString( "ChatMessage" );

	// Add our "npc to xp" table...
	local class = {
		// Enemy Classes and XP to gain...
		npc_zombie				= 10;
		npc_fastzombi			= 10;
		npc_fastzombie_torso	= 4;
		npc_headcrab_poison		= 12;
		npc_zombie_torso		= 3;
		npc_zombin				= 15;
		npc_headcrab			= 6;
		npc_headcrab_fast		= 5;
		npc_headcrab_black		= 9;
		npc_antlion				= 7;
		npc_antlionguard		= 350;

		// Default is our fall-back method..
		default					= 1;
	};


	//
	// With hooks we only want to return non-nil for specific behavior.. You returned nil so you're fine but I condensed it all to 1 line..
	//
	hook.Add( "PlayerDeath", "XPOnNPCDeath", function( victim, killer, _w )
		if ( IsValid( victim ) && IsValid( killer ) && killer:IsPlayer( ) && victim:IsNPC( ) ) then
			// Use the npc class to xp table above for xp if the npc exists, otherwise use the default value...
			local _xp = class[ victim:GetClass( ) ] || class.default;

			// Add XP
			killer:AddXP( _xp );

			// Network the chat-message
			killer:ChatMessage( "+" .. _xp .. " EXP Added!", Color( 0, 155, 0, 255 ) );
		end
	end );
else
	//
	// This is the CLIENT side
	//


	//
	// Add a networking receiver for ChatMessage...
	//
	net.Receive( "ChatMessage", function( _len )
		// Display the chat message...
		LocalPlayer( ):ChatMessage( net.ReadString( ), net.ReadColor( ) );
	end );
end
	

Thanks for the reply, this looks a lot more efficient and overall better. I am getting an odd error though. This is how I am shoving it in:

Here is the error:
[ERROR] LuaCmd:1: attempt to concatenate global ‘class’ (a nil value)

  1. unknown - LuaCmd:1

It’s because you pasted code at the end of the file instead of including it where it should be. Additionally, Facepunch is breaking code-view again so here is my post from above:

https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/simple_addxp_on_kill_system/simple_addxp_on_kill_system.lua.html

https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/simple_addxp_on_kill_system/simple_addxp_on_kill_system_optimized.lua.html

Remove .html to view .lua…

And, here’s the third one with your code inserted properly… except I set it so killer gets 1500 xp and others get 350… This could be done by using net.Broadcast or net.SendExcluding or whatever but I left it in your general design with a few changes to make it more “dynamic”…

https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/simple_addxp_on_kill_system/simple_addxp_on_kill_system_extra.lua.html

remove .html to view .lua as html doesn’t copy well…

Also, review yours, then review mine and see how I used hook.Add( …, function( ) … end ); instead of defining a function which sits in global memory… It becomes “local” to the hook… You did define a local function in your method which is good; my personal choice simply sets it all up in one go which reduces line-count by 1 but looks cleaner in my opinion… The “Extra” file did move the net message out into a local function because with the way you did things it needed to give xp and send a chat message multiple times on boss so…

Edit: Modified a bit more to fix the message output: https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/simple_addxp_on_kill_system/simple_addxp_on_kill_system_extra.lua