Run a client side command from server

Hi there
I was just wondering if someone could help me out here.
I want to make a ulx command (say !changeserver) that will force all the players connected on one of my servers to connect to my other server.

I’ve been trying to do something with the Player.ConCommand function and the command: connect <IP> but its not working.

Can someone help me out here?

I found this on the ulx forums forever ago but it should still work!

The creator of that is Eccid, but from what I can see it still works or you can try this addon that someone cloned a while back https://github.com/Tomelyr/Server-Hopping-Bunny/tree/master/server-hopping-bunny

So if an admin types this it will force everyone onto the server or just the person who typed it?
Because reading over it, it looks like its the latter, I may be wrong (not too great with lua)

Right now its just the person who types it, But when I actually get to sit down at my computer later I could try to work on one that would force everyone to run the command.

EDIT: If your looking for a full server redirect that redirects on join I have that too!

Bump I really need help with this.

use



for k, v in pairs(player.GetAll()) do
--code
end


to loop through everyone

Should this work?



concommand.Add("changeserver", function( ply )
	for k, v in pairs(player.GetAll()) do
        ply:ConCommand("connect 74.91.119.84:27015")
        end
end)


Change up that ply to a v



concommand.Add("changeserver", function( v )
	for k, v in pairs(player.GetAll()) do
        ply:ConCommand("connect 74.91.119.84:27015")
        end
end)


And now for my noobiest question, this is all I need to put into a lua file and it should work or is there more needed?

Well at the minute i could join your server and run that command, you may want to make it admin only if you are going to be reconnecting all of the players.

Also you need to change the ply in the loop to a v, no function args are required unless you are going to check if the player calling the command has a certain quality i guess you would call it

the ply:ConCommand should be v:ConCommand?
And I’m so sorry to be asking so many questions, but how do I make it an admin only command?




concommand.Add("changeserver",function(ply) -- The player who is running the command

   if(!ply:IsAdmin()) then return end -- Block non admins from doing the command.

  for k,v in pairs(player.GetAll()) do -- v being the player object in this case
     v:ConCommand("connect iphere") -- Run the console command on them
  end -- close / end the loop

end)



Possible that there is errors here as i did it in browser :<

Thank you so much, I really appreciate it :slight_smile:

That code wouldn’t work; the function hook for concommand.Add requires 4 arguments, not just 1. Ideally, you should also want to include the ip to connect to as an argument to the command. Here’s a better version of the code;

[lua]
concommand.Add(“changeserver”, function(ply, _, args, _)
if ply:IsAdmin() and #args >= 1 then
for k, p in pairs(player.GetAll()) do
p:ConCommand("connect " … args[1])
end
end
end)
[/lua]

Here’s the problem here:
AIX-Who’s code only changed the server for the 1 person who typed it in
Maurdekye’s code does nothing

What’s wrong here???

You have to add the ip of the server as an argument to the command.

Example: changeserver 192.168.1.54

Did that, and now, same problem as before, only the 1 person who types it changes.

You’ve tested this? Did you get multiple people to join one server with you, run the command for another server, and then check to see if anyone is left on the first one?
Did you get another player to run the command and see what happens?

Yep, tried all that, it only connects the person who puts it in the console.

Seems like you’ve put the code client side, make sure it’s server side. For example, garrysmod/lua/autorun/server/file.lua