Author Topic: STEAMID Targeting in Same Command  (Read 176 times)

0 Members and 1 Guest are viewing this topic.

Offline Rice Cooker

  • Newbie
  • *
  • Posts: 5
  • Karma: 0
STEAMID Targeting in Same Command
« on: July 11, 2019, 12:39:57 AM »
What I am wondering right now is there a way to have a command have then option to target someone in game through their name or through their STEAMID without creating a separate command for the STEAMID targeting.

Ex:
Command: Jail
Targeting: Name Or STEAMID
Result: If the person is online then they are jail. If not tsayerror notify player is not found.

Thanks!


Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2693
  • Karma: 419
    • |G4P| Gman4President
Re: STEAMID Targeting in Same Command
« Reply #1 on: July 11, 2019, 06:26:07 PM »
You could absolutely do this.

The say you would do it is in your parameters where you accept the player (name/id) have that just expect a string.

Inside of your function, do a string.left on the string to search for "STEAM_" to determine if the info given is a steam id.

If it is not, assume player name and use ULib.getUser("playername") to return the user for that player.
If it is... you could iterate through the list of online players to see if any of their steamid's match the given steamid.

for k, v in pairs( player.GetAll() ) do
  if v:SteamID() == passed_name_or_id then
    v:DoThings()
    break
  end
end

Let me know if you need more help with this.

Offline Rice Cooker

  • Newbie
  • *
  • Posts: 5
  • Karma: 0
Re: STEAMID Targeting in Same Command
« Reply #2 on: July 13, 2019, 01:06:34 AM »
Thanks MrPresident!

Would you be able to provide an example? I don't know how I would format the command function. I understand the fundamentals of how the command would work but some things like ( calling_ply, steamid, minutes, reason ) formatting have me a bit confused.


Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2693
  • Karma: 419
    • |G4P| Gman4President
Re: STEAMID Targeting in Same Command
« Reply #3 on: July 13, 2019, 09:58:45 AM »
This is rough.. and untested.. but it should work. you'll just need to edit it to your liking.

The way I told you to do it would work.. but this way would be better. There is a function in gmod called player.GetBySteamID that will work better than looping through the players checking their IDs. :)

Code: [Select]
local CATEGORY_NAME = "Fun"

function ulx.test_command( calling_ply, target_player )

local ply = nil
if player.GetBySteamID( target_player ) then ply = player.GetBySteamID( target_player ) end
if ULib.getUser( target_player ) then ply = ULib.getUser( target_player )

if IsValid( ply ) then
--Do some things here. ply is the player object of the player we found either by steamid or name
else
--Do some things here. No player found either by steamid or name
end

end
local test_command = ulx.command( CATEGORY_NAME, "ulx test", ulx.test_command, "!testcommand" )
test_command:addParam{ type=ULib.cmds.StringArg, hint="Player Name or SteamID", ULib.cmds.takeRestOfLine }
test_command:defaultAccess( ULib.ACCESS_ADMIN )
test_command:help( "Test Command" )

Offline Rice Cooker

  • Newbie
  • *
  • Posts: 5
  • Karma: 0
Re: STEAMID Targeting in Same Command
« Reply #4 on: July 14, 2019, 06:59:44 PM »
Thanks Again MrPresident!

The example is outputting an error.
This error being : bad argument #1 to 'upper' (string expected, got nil)

 Would you happen to know what might be able to fix this

Example command:
Code: [Select]
local CATEGORY_NAME = "TEST"

function ulx.test_command( calling_ply, target_player )

local ply = nil
if player.GetBySteamID( target_ply ) then ply = player.GetBySteamID( target_ply ) end
if ULib.getUser( target_ply ) then ply = ULib.getUser( target_ply ) end

if IsValid( ply ) then
target_ply:SendLua([[RunConsoleCommand("retry")]])
ulx.fancyLogAdmin( calling_ply, "#A has reconnected #T", target_ply )
else
ULib.tsayError( calling_ply, "ERROR.", true )
end

end
local test_command = ulx.command( CATEGORY_NAME, "ulx test", ulx.test_command, "!testcommand" )
test_command:addParam{ type=ULib.cmds.StringArg, hint="Player Name or SteamID", ULib.cmds.takeRestOfLine }
test_command:defaultAccess( ULib.ACCESS_ADMIN )
test_command:help( "Test Command" )

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8007
  • Karma: 377
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: STEAMID Targeting in Same Command
« Reply #5 on: July 14, 2019, 09:52:34 PM »
I'm not MrP, but I can answer.
In your function, you setup target variable, target_player, but later reference only target_ply
The price one pays for pursuing any profession or calling is an intimate knowledge of its ugly side. - James Baldwin