"attempt to index global 'chat' (a nil value)"

I want to use chat.AddText for some things in an script i’m writing, however for some reason no matter what I it just shows me this error:


[ERROR] lua/pointshop/items/sniper/weapon_zm_rifle_ps.lua:5: attempt to index global 'chat' (a nil value)
  1. OnBuy - lua/pointshop/items/sniper/weapon_zm_rifle_ps.lua:5
   2. PS_BuyItem - lua/pointshop/sv_player_extension.lua:254
    3. func - lua/pointshop/sv_init.lua:13
     4. unknown - lua/includes/extensions/net.lua:32


From this code:


function ITEM:OnBuy(ply)
	chat.AddText( Color( 100, 100, 255 ), ply, ", how are you doing?" )
	ply:Give(self.WeaponClass)
end

and/or


concommand.Add( "chattext", function(ply)
	chat.AddText( Color( 100, 100, 255 ), ply, ", how are you doing?" )
end )

Yes, I’ve tried


if CLIENT then

It doesn’t do anything then.

I’m really irritated, because I’ve used chat.AddText before and it worked. What’s wrong now?

–Santifocus

Try


if CLIENT then

again.


function ITEM:OnBuy(ply)
    if (CLIENT) then
        chat.AddText( Color( 100, 100, 255 ), ply, ", how are you doing?" )
    else
        ply:Give(self.WeaponClass)
    end
end

[editline]13th November 2017[/editline]

are pointshop items even shared?

Dunno if they are but doing your code gives you the weapon so I guess they are not.
What about the concommand.Add? Because it doesn’t work either

ITEM:OnBuy only executes serverside on Pointshop, chat is a clientside library.

You can either go for a simple Player:ChatPrint(string) or if you really need coloured chat text try using something like this.

You are running the code in the server realm (indicated by call stack starting from a serverside file). The Chat library is only available clientside, and calling it serverside will produce an error. A console command added in the server realm will only be called on the server, therefore the same error would also be produced from calling a serverside command that requires a clientside library.

I don’t know much about pointshop, or whether it allows items to define clientside code, but if not, or if you are in a rush, simply network it using the Net library, and call your chat library functions clientside.

[editline]13th November 2017[/editline]

Ninja’d :frowning:

Tyvm! Matt helped me out twice now ^^