Author Topic: Command help  (Read 128 times)

0 Members and 1 Guest are viewing this topic.

Offline Dolph1n

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
    • View Profile
Command help
« on: January 07, 2017, 05:21:38 PM »
So, I'm not the best at coding, but I know basics and I'm pretty good at using ctrl+c and ctrl+v, so go easy on me.

Anyways I'm trying to make a command  for TTT that will check if you are alive, and if you are will give you a Tsay, and if you're NOT alive it will force you to spectate a player(like ulx spectate does).

Here's what I have so far:
Code: [Select]
function ulx.spectate( calling_ply, target_ply )
if not calling_ply:IsValid() then
Msg( "You can't spectate from dedicated server console.\n" )
return
end
for k,v in pairs(calling_plys) do
if v:Alive() then
Msg( "You can't spectate while alive!" )
else
calling_ply:Kill()
calling_ply:SetForceSpec(true)
calling_ply:SetTeam(TEAM_SPEC)
calling_ply:ConCommand( "ttt_spectate" )
calling_ply:ConCommand( "ttt_spectator_mode 1" )
calling_ply:Spectate( OBS_MODE_IN_EYE )
calling_ply:SpectateEntity( target_ply )



ulx.fancyLogAdmin( calling_ply, true, "#A began spectating #T", target_ply )
end
end
end

When I use the command, nothing happens and I get this error:
Code: [Select]
[ERROR] addons/ulx/lua/ulx/modules/sh/util.lua:200: function arguments expected near ':'
  1. unknown - addons/ulx/lua/ulx/modules/sh/util.lua:0

EDIT: I think I got it, retrying.

Ok, so I no longer get the error but nothing happens, at least while alive.

EDIT 2: Nothing happens while dead either.

EDIT 3: It's no longer a recognized command by ULX. Fun.

EDIT 4: There's gonna be a lot of edits in this. Wow I'm bad at coding.

I moved some "end"s around and got it to be a command again, but now I get this error:
Code: [Select]
[ERROR] addons/ulx/lua/ulx/modules/sh/util.lua:196: bad argument #1 to 'pairs' (table expected, got nil)
  1. pairs - [C]:-1
   2. call - addons/ulx/lua/ulx/modules/sh/util.lua:196
    3. __fn - addons/ulib/lua/ulib/shared/commands.lua:943
     4. unknown - addons/ulib/lua/ulib/shared/commands.lua:1296
      5. unknown - lua/includes/modules/concommand.lua:54
« Last Edit: January 07, 2017, 06:17:30 PM by Dolph1n »

Offline iViscosity

  • Sr. Member
  • ****
  • Posts: 474
  • Karma: 16
    • View Profile
    • iViscosity Gaming
Re: Command help
« Reply #1 on: January 07, 2017, 08:49:03 PM »
Code: Lua
  1. function ulx.spectate( calling_ply, target_ply )
That is your function statement.

Code: Lua
  1. for k,v in pairs(calling_plys) do
This is the line giving the error.


Nowhere in your code do you have a 'calling_plys' variable set. pairs requires a table as an argument, and because calling_plys is not defined anywhere (hint: nil) the function returns nil. The error thrown says exactly that, it expects a table but a nil statement was given.


I don't really understand why you're using a for k,v loop, because when someone calls a command, there's only going to be one person calling it, and one target. So really, you can remove the whole for loop, check if calling_ply:Alive() then he can't do it, else he can.


EDIT: Just realized your error pointed to the default ULX location. While you can do this, this is a bad idea because when ULX and ULib are updated, your command is going to be overwritten. Put it in something like
Code: [Select]
addons/<yourAddonName>/lua/ulx/modules/sh

EDIT2: Msg only echoes to the Dedicated Server console (the RCon). To get it to go to the player, you'll need to use Player:PrintMessage or ULib.tsay
« Last Edit: January 07, 2017, 08:55:46 PM by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me a PM or Join my Discord!.
--==[ Links ]==--
Steam
YouTube
Steam Group (feel free to join!)
Forums
Public Discord
Teamspeak

Quote from: Steve Jobs
Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking until you find it. Don't settle.

Offline Dolph1n

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
    • View Profile
Re: Command help
« Reply #2 on: January 08, 2017, 12:17:55 AM »
Code: Lua
  1. function ulx.spectate( calling_ply, target_ply )
That is your function statement.

Code: Lua
  1. for k,v in pairs(calling_plys) do
This is the line giving the error.


Nowhere in your code do you have a 'calling_plys' variable set. pairs requires a table as an argument, and because calling_plys is not defined anywhere (hint: nil) the function returns nil. The error thrown says exactly that, it expects a table but a nil statement was given.


I don't really understand why you're using a for k,v loop, because when someone calls a command, there's only going to be one person calling it, and one target. So really, you can remove the whole for loop, check if calling_ply:Alive() then he can't do it, else he can.


EDIT: Just realized your error pointed to the default ULX location. While you can do this, this is a bad idea because when ULX and ULib are updated, your command is going to be overwritten. Put it in something like
Code: [Select]
addons/<yourAddonName>/lua/ulx/modules/sh

EDIT2: Msg only echoes to the Dedicated Server console (the RCon). To get it to go to the player, you'll need to use Player:PrintMessage or ULib.tsay
What about something like this?
Code: [Select]
function ulx.spectate( calling_ply, target_ply )
if not calling_ply:IsValid() then
msg( "You can't spectate from dedicated server console.\n" )
return
end
for v in pairs(calling_ply) do
if v:Alive() then
calling_ply:PrintMessage( "You can't spectate while alive!" )
table.insert(affected_plys, v)
else
calling_ply:Kill()
calling_ply:SetForceSpec(true)
calling_ply:SetTeam(TEAM_SPEC)
calling_ply:ConCommand( "ttt_spectate" )
calling_ply:ConCommand( "ttt_spectator_mode 1" )
calling_ply:Spectate( OBS_MODE_IN_EYE )
calling_ply:SpectateEntity( target_ply )



ulx.fancyLogAdmin( calling_ply, true, "#A began spectating #T", target_ply )
end
end
end

Offline iViscosity

  • Sr. Member
  • ****
  • Posts: 474
  • Karma: 16
    • View Profile
    • iViscosity Gaming
Re: Command help
« Reply #3 on: January 08, 2017, 10:54:00 AM »
That should work, however the 'msg' at the top as it is a nil value, it is case sensitive, Msg.


EDIT: Also, you can use  to highlight your lua.

Sent using Tapatalk. Owner of iViscosity Gaming.

« Last Edit: January 08, 2017, 10:56:20 AM by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me a PM or Join my Discord!.
--==[ Links ]==--
Steam
YouTube
Steam Group (feel free to join!)
Forums
Public Discord
Teamspeak

Quote from: Steve Jobs
Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking until you find it. Don't settle.