Can't add hooks using GM

I made a script that’s supposed to override the default join/leave chat messages and display my own. This is the hook that’s supposed to override the messages;



function GM:ChatText(_, __, text, type)
	if type == "joinleave" then return true end
end


However, when I try to run the script on a server, I get an error attempting to index the global ‘GM’, which is a nil value. Why is that?
I also tried doing it the other way using hook.Add, but it doesn’t override the messages.



hook.Add( "ChatText", "hide_joinleave", function( index, name, text, typ )
	if ( typ == "joinleave" ) then return true end
end )


This worked for me?

I said that I tried that; it doesn’t override the messages.

You’re using GM in an addon. When creating addons you’ll want to use hook.Add…

If you create NEW GM functions ( only use GM in addons if you create brand NEW functions, never to overwrite; only use hook.Add in addons ) then you will need to add them in hook.Add( “Initialize” …

because addons execute prior to game-modes being loaded meaning GM / GAMEMODE isn’t loaded yet. By running on Initialize, you can use GM or GAMEMODE ( I believe GAMEMODE would be the one to use at that point ).

hook.Add, when you return non-nil, will prevent the GM function from executing ( and any other hooks under the same category ) so what Invule posted should work just fine.

Well, it doesn’t work. So I don’t know what to do.

There may be an issue with the wiki page if it isn’t overwriting the right message.

Also, you haven’t answered whether or not it is a custom chat-box ( not all custom chat-boxes overwrite all hooks or call all GM functions as the original chat-box does, a poor decision from the developer meaning all other mods won’t work )

So, the next step is basic debugging:

I’d recommend printing out the values of each variable, and seeing if type == “joinleave” when a player joins / leaves ( should be the same for bot ).

It’s not a custom chat box; it’d probably be best to assume that since I didn’t specify.

I’m debugging it right now, and it doesn’t even seem to be printing anything…
Here’s the function hook I made to test it;



function blocktext(id, name, text, type)
	PrintMessage(2, " | Message type " .. type .. ": \"" .. text .. "\"")
end
hook.Add( "ChatText", "ChatText_", blocktext) 


Is that file clientside becaust ChatText is a clientside hook. It may even be a broken hook.

No, it’s a serverside file. Huh.
Is there a serverside alternative to ChatText? It’s what I was told to use by someone else on the forums.

Just put it in autorun/client instead of autorun/server man

[editline]28th February 2015[/editline]

and use hook.Add

I have a feeling that the OP wants to block the output on the SERVER, not the CLIENT, so the console.log won’t use the default messages in addition to the ops.

I’d have to wait until I get back to SC to verify if I did that on mine ( I do have custom outputs for different phases of joining but I’m not sure if I removed the original messages ) and to give an answer. The only console.log I have doesn’t have a log of any players joining ( set up code to remove console.log each time I start the server and I didn’t join last time I launched it because I was testing a start-up system )…

If anyone else has the answer, then that’d be helpful.

I’ll make a separate thread, since my problem is different.