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

0 Members and 1 Guest are viewing this topic.

Offline Rice Cooker

  • Newbie
  • *
  • Posts: 8
  • 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: 2728
  • Karma: 430
    • |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: 8
  • 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: 2728
  • Karma: 430
    • |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: 8
  • 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: 8096
  • Karma: 390
  • 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
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Rice Cooker

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: STEAMID Targeting in Same Command
« Reply #6 on: July 20, 2019, 01:27:02 AM »
Thanks JamminR!

I still dont seem to be able to get this to function properly.
I have changed the function to just fancyadmin but I am still getting this error:
[ERROR] lua/includes/extensions/string.lua:297: attempt to call global 'error' (a nil value)

Ex Code:
Code: [Select]
local CATEGORY_NAME = "Fun"

function ulx.test_command( calling_ply, target_ply )

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
  ulx.fancyLogAdmin( calling_ply, "#A has tried the test command on #T", target_ply )
else
ULib.tsayError( calling_ply, "The Command Is Broken!", 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 Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: STEAMID Targeting in Same Command
« Reply #7 on: July 20, 2019, 03:11:35 AM »
In your command target_ply does not hold a Player but a string ("Player Name or SteamID"). The Player instance was stored in the ply variable instead.

Format specifier #s formats a string:
Code: [Select]
ulx.fancyLogAdmin( calling_ply, "#A formatted #s", target_ply )
Format specifier #T formats a Player, or a table of Players:
Code: [Select]
ulx.fancyLogAdmin( calling_ply, "#A formatted #T", ply )


Fun fact: ULX commands support SteamID targeting out-of-the-box. See "ulx help" for more info on how to use keyword targeting.
Code: [Select]
ulx slay $STEAM_0:1:0

Offline Rice Cooker

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: STEAMID Targeting in Same Command
« Reply #8 on: July 20, 2019, 03:34:15 AM »
Thanks Timmy!

I appreciate the help. I understand that ULX already has steamid targeting within keyword targeting but I would like to have all commands being able to target without having to place a symbol. It would allow all staff to be able to utilize the commands without having to be aware of ULX targeting.