Run a client side command from server

As I thought, you can’t use ply:ConCommand(“connect iphere”) serverside. Connect is one of the console commands that the server can’t run on the player. However, what you can do is broadcast the code to execute the command so that it is run clientside. Here’s something I made real quick to demonstrate:



function ChangeAllPlayersToServer()
	if SERVER then
		BroadcastLua([[LocalPlayer():ConCommand('connect IPADDRESSHERE')]]) -- Replace IPADDRESSHERE with the IP of your server. You can use ply:SendLua() for one person.
	else
		net.Start('ConnectAllPlayers')
		net.SendToServer()
	end
end

concommand.Add('allconnect', function(ply, cmd, args, argstr) -- Change the command name from "allconnect"
	if IsValid(ply) and not ply:IsSuperAdmin() then ply:ChatPrint('You do not have the permission to run this command.') return end -- This does not return at all if it is run serverside as player is not valid.
	ChangeAllPlayersToServer()
end)

if SERVER then
	util.AddNetworkString('ConnectAllPlayers')
	net.Receive('ConnectAllPlayers', ChangeAllPlayersToServer)
end


Edit:
You could probably just broadcast a net message and add a receiver that runs ply:ConCommand() too, but this is simpler.

Lua is not strict on how many arguments you define.

-snip-

You sir, deserve a cookie.
It works perfectly.
Thank you so much!!!

That code effectively allows anyone on the server to force every player to connect to the server you specify :v:

you’re filtering based on rank in the command, not in the net.Receive function

Forgot to do server-side checks for net messages. Sorry, that’s on me.



function ChangeAllPlayersToServer()
	if SERVER then
		BroadcastLua([[LocalPlayer():ConCommand('connect IPADDRESSHERE')]]) -- Replace IPADDRESSHERE with the IP of your server. You can use ply:SendLua() for one person.
	else
		net.Start('ConnectAllPlayers')
		net.SendToServer()
	end
end

concommand.Add('allconnect', function(ply, cmd, args, argstr) -- Change the command name from "allconnect"
	ChangeAllPlayersToServer()
end)

if SERVER then
	util.AddNetworkString('ConnectAllPlayers')
	net.Receive('ConnectAllPlayers', function(len, ply)
		if not ply:IsSuperAdmin() then ply:ChatPrint('You do not have the permission to run this command.') return end
		ChangeAllPlayersToServer()
	end)
end


There we go. I may still be in the wrong, but I edited it a little bit.