Connect to server command

Ok so I want to make a code that when you say “/TTT” it connects you to the server

This is what I have so far.
This is in the addons folder in the server
addons\Commandsss\lua\sncommands\cl\cl_initi.lua

Here is the code


local Button

hook.Add( "PlayerSay", "PlayerSayExample", function( ply, text, team, Button )
	if ( string.sub( text, 1, 4 ) == "/darkrp" ) then
vgui.Create( "DButton" )
Button:Center
Button:MakePopup()								
Button:SetSize( 200, 50 )							
Button:SetText( "Join Our DarkRP" )							 
Button:SetConsoleCommand( "connect", "IP" )
end
	end

PlayerSay is serverside. Either network from the server to client, or use OnPlayerChat, although I vaguely remember there being a reason to not use that hook for commands.

So would I change it to a local function or a function? Im a starter at this.

Just noticed there were a few more issues with your code. Try this:


-- OnPlayerChat is the clientside equivalent of PlayerSay
hook.Add( "OnPlayerChat", "PlayerSayExample", function(ply, text)
	-- Only make it work for the player who types it
	-- Also, using a strict equality check instead of a sub-string
	-- Makes sure /darkrpblahblah doesn't work
	if ( ply == LocalPlayer() and text:lower() == "/darkrp" ) then
		local Button = vgui.Create( "DButton" )
		Button:Center()
		Button:MakePopup()
		Button:SetSize( 200, 50 )							
		Button:SetText( "Join Our DarkRP" )							 
		Button:SetConsoleCommand( "connect", "IP" )
	end
end )

I commented in the fixes.

Hm dosnt seem to open the button. I see what I did wrong, how should I know when its server or clientside?