Serverlist by typing /server

I was wondering on how I would make it so if someone types /server in chat (on Darkrp) it will bring up a server list that will have servers to connect to such as my TTT server. Any help would be appreciated. I would also like to know how to do it in TTT. If this is too much to ask for, would anyone know a code in main.lua (darkrp) that would cause typing /ttt into chat automatically connect you to the TTT server? Thanks

A function creating few vgui panels with buttons on it hooked to PlayerSay.
Same for TTT, but instead of vgui panels, it would be ply:ConCommand(“connect serverip”)

So would something like the following work if I was to make it so /ttt would automatically connect you to the ttt server?


local function ttt(ply)
	if ply:IsUser() then
		ply:ConCommand("connect 74.91.116.24:27015")
	end
end
AddChatCommand("/ttt", ttt)
AddChatCommand("/TTT", ttt)
AddChatCommand("/servers", ttt)


ply:IsUser does not exist, use: IsValid(ply).

Also, AddChatCommand does not exist, and you need to hook it to player say, here try something like this:


function TTTServer(pl,text,public)
	if string.StartWith(text,"/ttt") == true or string.StartWith(text, "/TTT") == true then
		if IsValid(pl) then
			pl:ConCommand("connect 74.91.116.24:27015")
		end
	end
end
hook.Add("PlayerSay","switchtoTTT",TTTServer)


Next, your /servers will not bring up a panel or a list of server it will simply just switch you to your TTT server, if you want it like this just add it to my code, but if not you need to look into derma and then making the /servers command into a derma panel.

Why do this IsValid thing anyway? It’s not like playersay would return something else than a player.
From the wiki: “GM:PlayerSay( Player sender, string messageContent, boolean isTeamChat )”

It can return nil is the say command was typed into dedicated server console.

[editline]3rd June 2013[/editline]

Or sometimes lag happens, on laggy servers.

Thanks, I will try this right now

[editline]3rd June 2013[/editline]

I changed it up a little and added the Chat Commands on the bottom because goosey’s code didn’t seem to work. Now it looks like this:



function TTTServer(pl,text,public)
	if string.StartWith(text,"/ttt") == true or string.StartWith(text, "/TTT") == true then
		if IsValid(pl) then
			pl:ConCommand("connect 74.91.116.24:27015")
		end
	end
end
hook.Add("PlayerSay","switchtoTTT",TTTServer)
AddChatCommand("/ttt", TTTServer)
AddChatCommand("/TTT", TTTServer)


I now can type /ttt or /TTT and it doesn’t show up (probably meaning it is a command now) but it doesn’t want to do anything. I get this in console after typing it:



FCVAR_SERVER_CAN_EXECUTE prevented server running command: connect


Help anyone?

iirc, you need to run the connect on the client not the server.

use net or a concommand on the client.

How do I do that?

Like this: http://wiki.garrysmod.com/page/Using_the_net_library

I read it but not understand what to replace and what to replace it with. Would it be net.start?

Dunno if this works:


SERVERSIDE:

util.AddNetworkString( "changeserver" )
function TTTServer(pl,text,public)
	if string.StartWith(text,"/ttt") == true or string.StartWith(text, "/TTT") == true then
		if IsValid(pl) then
			net.Start( "changeserver" )
			net.Send(pl)
		end
	end
end
hook.Add("PlayerSay","switchtoTTT",TTTServer)
AddChatCommand("/ttt", TTTServer)
AddChatCommand("/TTT", TTTServer)

CLIENTSIDE:

net.Receive( "changeserver", function()
        LocalPlayer():RunConsoleCommand("connect", "IPOFYOURSERVER")
end)


Where do I put the Clientside code at? which lua file?

serverside is init.lua and clientside is cl_init.lua

Next time, either learn lua yourself first or hire a coder. You wont be able to get stepped through like this for ever.

Close but no cigar




[ERROR] gamemodes/darkrp/gamemode/cl_init.lua:522: attempt to call method 'RunConsoleCommand' (a nil value)
  1. func - gamemodes/darkrp/gamemode/cl_init.lua:522
   2. unknown - lua/includes/modules/net.lua:31



Wouldn’t it be ConCommand?

[editline]3rd June 2013[/editline]

Got it. fixed client side to:



net.Receive( "changeserver", function()
        LocalPlayer():ConCommand("connect IPOFYOURSERVER")
end)


Thank you

You don’t need LocalPlayer() when calling RunConsoleCommand(), sorry forgot about that.

would it be the same for a TTT server to make it connect to Darkrp? Just change ttt to darkrp in the code and ofcourse the IP?

yes

I did that but it’s not working