Garrys Mod chat.AddText problem!

Right I am trying to create a NPC for Garry’s Mod that you can press ‘E’ on and it says in chat “Hello (Player name)!” and then it opens a derma window where you can purchase health. The thing is that for some reason I can not get the text to appear in chat. I have used the garrys mod wiki link : http://wiki.garrysmod.com/page/chat/AddText and it still doesn’t seem to work. Here is the code so far:

Imgur

As you can see under the ENT:Use function it says 'chat.AddText(“Hello World”) <-- This was just saving me typing a big long string for it then not to work. But whenever I walk up to the NPC and press ‘E’ all it does is set my health and doesn’t put the text in chat ? The error that I get is:

[ERROR] lua/entities/healthnpc.lua:39: attempt to index global ‘chat’ (a nil value)

  1. unknown - lua/entities/healthnpc.lua:39

I have looked all over to try to find the answer to this error but cannot so please can someone help!

I don’t know what I have done wrong in the code but it is probably something that is easy to solve.

Thank you for any support you give and thank you for at least reading the post!

-Shadow

Use is serverside, chat.AddText and derma is clientside

Look into using the net library especially since you’re going to need it for giving health on pressing a button

Wait I have one file called “healthnpc.lua” in the lua/entities directory! How do I differentiate between client and server ?

[editline]30th January 2016[/editline]

Wait I have one file called “healthnpc.lua” in the lua/entities directory! How do I differentiate between client and server ?

Thank you for your reply! :smiley:

if CLIENT then

end

if SERVER then

end

or you can use an else statement, depends on how your code is written

Thank you.

Should I use code like this then :

function ENT:Use(a , c)
if CLIENT then
chat.AddText(“Hello “…c:Nick()” !”
end
if c:Health() <100 then
c:SetHealth(100)
end
end

Again thank you for your reply!

Please can someone respond I am really wanting to learn how to code for garrys mod server and this is a base for me! Greatly appreciated!



if CLIENT then
	
	CLIENT side code goes here
	
end

if SERVER then

	SERVER side code goes here

end


Would this be the right code to do chat.AddText ?

Please just read through this

I now understand how to do chat.AddText but one last thing then I was mark this as solved, what directory do I put all of the code in ? Do I create a new entity place it down then it will work ?

All your help has been greatly appreciated!