• Print

Author Topic: Server redirecter  (Read 8442 times)

0 Members and 1 Guest are viewing this topic.

Offline cyborgguy123

  • Newbie
  • *
  • Posts: 4
  • Karma: 0
Server redirecter
« on: April 07, 2017, 11:32:35 am »
I have this code to redirect users to another server but it comes up with the error bad server address even though the IP is clearly valid.

Here is the code (note this is not my code before everyone here roasts me):

hook.Add( "PlayerSay", "Server Redirecter", function(ply, text)
   if (string.lower(text) == "!newserver" ) then
      ply:SendLua([[RunConsoleCommand("connect", "IP.ADRESS.GOES.HERE")]])
      return ""
   end
end)
« Last Edit: April 07, 2017, 11:41:50 am by cyborgguy123 »

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Server redirecter
« Reply #1 on: April 07, 2017, 12:15:05 pm »
From the two quick releases I found searching our forums for  examples of similar function code;
Server "Hopper" + Platform - /index.php/topic,8609.0.html#msg44234 />Simple hopper - /index.php/topic,8464.0.html

You're missing the port of the server IP
connect ip:port#
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline cyborgguy123

  • Newbie
  • *
  • Posts: 4
  • Karma: 0
Re: Server redirecter
« Reply #2 on: April 07, 2017, 12:22:30 pm »
From the two quick releases I found searching our forums for  examples of similar function code;
Server "Hopper" + Platform - /index.php/topic,8609.0.html#msg44234 />Simple hopper - /index.php/topic,8464.0.html

You're missing the port of the server IP
connect ip:port#
It still gives the same error with the port, and I don't want anything too complicated. I don't need derma panels or anything else. I just need a simple command to connect someone to a different server.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Server redirecter
« Reply #3 on: April 07, 2017, 01:55:09 pm »
Why don't you just do
Code: Lua
  1. ply:ConCommand( "connect SERVER.IP.ADDRESS:PORT" )
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Server redirecter
« Reply #4 on: April 08, 2017, 01:19:30 am »
RunConsoleCommand wraps command arguments in quotes before it uses them.

Resulting in connect "127.0.0.1:27015" instead of connect 127.0.0.1:27015.

For most console commands, the quotes are fine. But the connect command will consider the quotes as a part of the IP address. That’s why using RunConsoleCommand with connect gives you the "Bad server address" error.



You can use Player:ConCommand if you don’t want arguments to be wrapped in quotes (example by iViscosity in the post above).

For security reasons, this still won’t work with connect. Garry’s Mod will prevent the server from calling that command on the client.

The way most scripts bypass this: forcing the client to call Player:ConCommand instead. In code, that would look like this:
Code: Lua
  1. ply:SendLua("LocalPlayer():ConCommand('connect 127.0.0.1:27015')")
« Last Edit: May 31, 2018, 02:18:07 am by Timmy »

Offline cyborgguy123

  • Newbie
  • *
  • Posts: 4
  • Karma: 0
Re: Server redirecter
« Reply #5 on: April 08, 2017, 07:00:12 am »
RunConsoleCommand wraps command arguments in quotes before it uses them.

Resulting in connect "127.0.0.1:27015" instead of connect 127.0.0.1:27015.

For most console commands, the quotes are fine. But the connect command will consider the quotes as a part of the IP address. That’s why using RunConsoleCommand with connect gives you the "Bad server address" error.

You can use Player:ConCommand if you don’t want arguments to be wrapped in quotes (example by iViscosity in the post above).

For security reasons, this still won’t work with connect. Garry’s Mod will prevent the server from calling that command on the client.

The way most scripts bypass this: forcing the client to call Player:ConCommand instead. In code, that would look like this:
Code: Lua
  1. ply:SendLua("LocalPlayer():ConCommand('connect 127.0.0.1:27015')")
Thank you for your help, the code works now :P

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Server redirecter
« Reply #6 on: April 08, 2017, 09:42:37 am »
Huh, didn't know about those stipulations with `connect`. Good to know.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Server redirecter
« Reply #7 on: April 08, 2017, 12:10:57 pm »
Huh, didn't know about those stipulations with `connect`. Good to know.
Welcome to the Source engine! :^)
bw81@ulysses-forums ~ % whoami
Homepage

  • Print