• Print

Author Topic: Nrslay, need some help  (Read 6066 times)

0 Members and 1 Guest are viewing this topic.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Nrslay, need some help
« on: August 29, 2016, 11:29:40 pm »
I'm writing a script just for grins and giggles, really, and I was wondering if this would work (it's a variant on Aslay, btw, using PData instead of SQL, because why not?)


Code: Lua
  1.    hook.Add( "PlayerDisconnected", "Warn Admins on slay leave/Auto ban on leave", function( ply )
  2.  
  3.  
  4.       if ply:GetPData( "slays_left" ) >= 1 then
  5.          
  6.          ulx.fancyLogAdmin( ply:Nick() .. " has left with " .. ply:GetPData( "slays_left" ) .. " slays left. They have been banned for (" ..  banTime .. ") minutes.")
  7.          ULib.addBan( ply:SteamID(), banTime, "Leave with " .. ply:GetPData( "slays_left" ) .. " slays left", ply:Nick(), "CONSOLE" )
  8.  
  9.  
  10.       end
  11.  
  12.  
  13.    end )
  14.  
  15.  
  16.    hook.Add( "PlayerAuthed", "Tell player how many slays they have left", function( ply, steamid, uniqueid )
  17.  
  18.  
  19.       if not ply:GetPData( "slays_left" ) == nil or 0 then
  20.          
  21.          ULib.tsayError( ply, "You have " .. ply:GetPData( "slays_left" ) .. " slays left." )
  22.  
  23.  
  24.       end
  25.  
  26.  
  27.    end
  28.  
  29.  
  30.    hook.Add( "TTTBeginRound", "Actually Slay", function()
  31.  
  32.  
  33.       for k,v in pairs( player.GetAll() ) do
  34.  
  35.  
  36.          if v:GetPData( "slays_left" ) >= 1 then
  37.            
  38.             v:Kill()
  39.             if IsValid( v.server_ragdoll ) then
  40.  
  41.  
  42.                local ply = player.GetByUniqueID( v.server_ragdoll.uqid )
  43.                if not IsValid( ply ) then return end
  44.                ply:SetNWBool( "body_found", true )
  45.                CORPSE.SetFound( v.server_ragdoll, true )
  46.                v.server_ragdoll:Remove()
  47.  
  48.  
  49.             end
  50.             v:SetPData( "slays_left", v:GetPData( "slays_left" - 1 ) )
  51.  
  52.  
  53.          end
  54.  
  55.  
  56.       end
  57.  
  58.  
  59.    end )
  60.  
  61. end

I have no real reason to make this, I just sorta felt like it because I was bored.


EDIT: This isn't the full script, just these hooks, the rest of the "ends" are for closing function and stuff..
« Last Edit: August 30, 2016, 03:33:47 pm by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Would this work?
« Reply #1 on: August 30, 2016, 06:46:06 am »
Why not just test it yourself? Testing it will give you a better, definitive answer much faster than asking people on the forums to look through the code or test it for you.

From a cursory inspection, it looks like it will not work. But to be sure, you should test it on a server. Also, if you can, avoid PData. It uses UniqueIDs which have collisions and also cannot be accessed when a player is not on the server. Moving away from PData will also fix what I see as the main problem, which is that you cannot get a disconnected player's PData.
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Would this work?
« Reply #2 on: August 30, 2016, 12:13:41 pm »
Why not just test it yourself? Testing it will give you a better, definitive answer much faster than asking people on the forums to look through the code or test it for you.

From a cursory inspection, it looks like it will not work. But to be sure, you should test it on a server. Also, if you can, avoid PData. It uses UniqueIDs which have collisions and also cannot be accessed when a player is not on the server. Moving away from PData will also fix what I see as the main problem, which is that you cannot get a disconnected player's PData.

I mean, I've used PData on similar things (permanent mutes, things like that). I guess I should've been more specific, can I do math with PData? Look at end of code, that's what I'm talking about.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Would this work?
« Reply #3 on: August 30, 2016, 01:24:41 pm »
In the last part you are subtracting a number from a string. That will not work. I'm not sure if it will error or what, but it probably won't be pretty. You probably meant to put the '- 1' outside the parentheses. This should work, but I would err on the side of caution and use tonumber on the result of GetPData and check that it doesn't return nil (tonumber returns nil if it can't convert the supplied value into a number).

Also, it looks like I was wrong (and you were right) about the player no longer existing once PlayerDisconnected was called.

However, I'm still curious as to why you didn't just try running this script to see if it would work.
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Would this work?
« Reply #4 on: August 30, 2016, 03:26:48 pm »
Lol I just didn't really feel like testing. Anyways, now that I am testing, I'm getting this:
Code: [Select]
[ERROR] addons/nrslay/lua/ulx/modules/autoban-slay.lua:10: attempt to compare number with boolean
  1. call - addons/nrslay/lua/ulx/modules/autoban-slay.lua:10
   2. __fn - addons/ulib/lua/ulib/shared/commands.lua:943
    3. unknown - addons/ulib/lua/ulib/shared/commands.lua:1296
     4. unknown - lua/includes/modules/concommand.lua:54

Code: Lua
  1. function ulx.nrslay( calling_ply, target_ply, rounds, reason )
  2.  
  3.    if not rounds >= 0 then -- This is line 10
  4.      
  5.       ULib.tsayError( calling_ply, "Invalid number of rounds. Must be 0 or higher." )
  6.  
  7.    elseif rounds == 0 then
  8.    
  9.       target_ply:RemovePData( "slays_left" )
  10.  
  11.    elseif rounds > 0 then
  12.      
  13.       target_ply:SetPData( "slays_left", rounds )
  14.  
  15.    else
  16.  
  17.       ULib.tsayError( calling_ply, "Unexpected error. Make sure you specified number of rounds." )
  18.  
  19.    end
  20.  
  21.  
  22. -- More stuff down here VVVV
  23.  
  24.  
  25.  
  26. local nrslay = ulx.command( "TTT Admin", "ulx nrslay", ulx.nrslay, "!nrslay" )
  27. nrslay:addParam{ type=ULib.cmds.PlayerArg }
  28. nrslay:addParam{ type=ULib.cmds.NumArg, hint="rounds", ULib.cmds.round, min = 0, default = 1 }
  29. nrslay:addParam{ type=ULib.cmds.StringArg, hint="reason", ULib.cmds.optional }
  30. nrslay:defaultAccess( ULib.ACCESS_OPERATOR )

this is the problematic part, why is it trying as a boolean and how do I make it take it as an integer?


Also, it's not showing in the XGUI.
« Last Edit: August 30, 2016, 03:34:36 pm by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Nrslay, need some help
« Reply #5 on: August 31, 2016, 08:42:55 am »
Do you get that error after running the command, or just after the file is loaded. The only way I can imagine it's getting a boolean is that it's being called from somewhere else (i.e. not being run due to a ULX command).
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Nrslay, need some help
« Reply #6 on: August 31, 2016, 08:47:25 am »
It happens when it's called. When opened at the start, I get no errors.


Code: [Select]
Adding Filesystem Addon 'd:\steam\steamapps\common\garrysmod\garrysmod\addons\nrslay'
Adding Filesystem Addon 'd:\steam\steamapps\common\garrysmod\garrysmod\addons\ulib'
Adding Filesystem Addon 'd:\steam\steamapps\common\garrysmod\garrysmod\addons\ulx'
Initializing Awesomium..
No Kinect SDK
Changing gamemode to Trouble in Terrorist Town (terrortown)
Detecting unused .gma files...
Finished! Run menu_cleanupgmas to clean up said files
Network: IP my.ip.kek, mode MP, dedicated No, ports 27015 SV / 27005 CL
Error loading cfg/trusted_keys_base.txt
Error loading cfg/pure_server_minimal.txt
PREP OK
///////////////////////////////
//      Ulysses Library      //
///////////////////////////////
// Loading...                //
//  shared/defines.lua       //
//  shared/misc.lua          //
//  shared/util.lua          //
//  shared/hook.lua          //
//  shared/table.lua         //
//  shared/player.lua        //
//  server/player.lua        //
//  shared/messages.lua      //
//  shared/commands.lua      //
//  server/concommand.lua    //
//  server/util.lua          //
//  shared/sh_ucl.lua        //
//  server/ucl.lua           //
//  server/phys.lua          //
//  server/player_ext.lua    //
//  server/entity_ext.lua    //
//  shared/plugin.lua        //
//  shared/cami_global.lua   //
//  shared/cami_ulib.lua     //
// Load Complete!            //
///////////////////////////////
[ULIB] Loading SHARED module: ulx_init.lua
///////////////////////////////
//       ULX Admin Mod       //
///////////////////////////////
// Loading...                //
//  sh_defines.lua           //
//  lib.lua                  //
//  base.lua                 //
//  sh_base.lua              //
//  log.lua                  //
//  MODULE: autoban-slay.lua //
//  MODULE: slots.lua        //
//  MODULE: uteam.lua        //
//  MODULE: votemap.lua      //
//  MODULE: xgui_server.lua  //
///////////////////////////////
// ULX GUI -- by Stickly Man //
///////////////////////////////
// Adding Main Modules..     //
//  bans.lua                 //
//  commands.lua             //
//  groups.lua               //
//  maps.lua                 //
//  settings.lua             //
// Adding Setting Modules..  //
//  client.lua               //
//  server.lua               //
// Adding Gamemode Modules.. //
//  sandbox.lua              //
// Loading Server Modules..  //
//  sv_bans.lua              //
//  sv_groups.lua            //
//  sv_maps.lua              //
//  sv_sandbox.lua           //
//  sv_settings.lua          //
// XGUI modules added!       //
///////////////////////////////
//  MODULE: chat.lua         //
//  MODULE: fun.lua          //
//  MODULE: menus.lua        //
//  MODULE: rcon.lua         //
//  MODULE: teleport.lua     //
//  MODULE: user.lua         //
//  MODULE: userhelp.lua     //
//  MODULE: util.lua         //
//  MODULE: vote.lua         //
//  end.lua                  //
// Load Complete!            //
///////////////////////////////
Included TTT language file: chef.lua
Included TTT language file: english.lua
Included TTT language file: german.lua
Included TTT language file: portuguese.lua
Included TTT language file: russian.lua
Included TTT language file: spanish.lua
Included TTT language file: swedish.lua
Included TTT language file: tradchinese.lua
Trouble In Terrorist Town gamemode initializing...
This is TTT version 2015-05-25
Using map cycle file cfg/mapcycle.txt.
Bad field in entity!!
Nav File is wrong or something (1)
Writing cfg/banned_user.cfg.


[L] Trouble in Terrorist Town
Map: ttt_minecraft_b5
Players: 1 / 4
Build: 6447
Server Number: 1


Garry's Mod


terrortown
ttt_minecraft_b5
4
76561198143462763
Requesting 2 lua files from the server
clientside lua startup!
[ULIB] Loading SHARED module: ulx_init.lua
[ULX] Loading CLIENT module: motdmenu.lua
[ULX] Loading CLIENT module: uteam.lua
[ULX] Loading CLIENT module: xgui_client.lua
[ULX] Loading CLIENT module: xgui_helpers.lua
[ULX] Loading CLIENT module: xlib.lua
[ULX] Loading SHARED module: chat.lua
[ULX] Loading SHARED module: fun.lua
[ULX] Loading SHARED module: menus.lua
[ULX] Loading SHARED module: rcon.lua
[ULX] Loading SHARED module: teleport.lua
[ULX] Loading SHARED module: user.lua
[ULX] Loading SHARED module: userhelp.lua
[ULX] Loading SHARED module: util.lua
[ULX] Loading SHARED module: vote.lua
Requesting texture value from var "$dummyvar" which is not a texture value (material: NULL material)
Included TTT language file: chef.lua
Included TTT language file: english.lua
Included TTT language file: german.lua
Included TTT language file: portuguese.lua
Included TTT language file: russian.lua
Included TTT language file: spanish.lua
Included TTT language file: swedish.lua
Included TTT language file: tradchinese.lua
TTT Client initializing...
Warning: Player issued command but is now vanished (Command was ""0.00"")
TTT initializing convar settings...
Client "iViscosity" spawned in server <STEAM_0:1:91598517> (took 4 seconds).


///////////////////////////////////////
//  ULX GUI -- Made by Stickly Man!  //
///////////////////////////////////////
// Loading GUI Modules...            //
//   bans.lua                        //
//   commands.lua                    //
//   groups.lua                      //
//   maps.lua                        //
//   settings.lua                    //
// Loading Setting Modules...        //
//   client.lua                      //
//   server.lua                      //
// Loading Gamemode Module(s)...     //
//   No module found!                //
// Modules Loaded!                   //
///////////////////////////////////////


TTT Client post-init...
Compact freed 479232 bytes
JOY_AXIS_X:  mapped to Turn (absolute)
JOY_AXIS_Y:  mapped to Look (absolute)
JOY_AXIS_Z:  unmapped
JOY_AXIS_R:  mapped to Forward (absolute)
JOY_AXIS_U:  mapped to Side (absolute)
JOY_AXIS_V:  unmapped
Advanced Joystick settings initialized
Redownloading all lightmaps
"ulx welcomemessage" = ""
<msg> - This is shown to players on join.
  CVAR generated by ULX
Server default language is:   english


Using on a listen server, this is the entire loadup.




Also, if it helps, this is where I defined the command:


Code: Lua
  1. local nrslay = ulx.command( "TTT Admin", "ulx nrslay", ulx.nrslay, "!nrslay" )
  2. nrslay:addParam{ type=ULib.cmds.PlayerArg }
  3. nrslay:addParam{ type=ULib.cmds.NumArg, hint="rounds", ULib.cmds.round, ULib.cmds.optional, default = 1, min = 0 }
  4. nrslay:addParam{ type=ULib.cmds.StringArg, hint="reason", ULib.cmds.optional }
  5. nrslay:defaultAccess( ULib.ACCESS_OPERATOR )
« Last Edit: August 31, 2016, 09:07:10 am by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Nrslay, need some help
« Reply #7 on: August 31, 2016, 12:11:49 pm »
What happens when you try to print rounds?
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Nrslay, need some help
« Reply #8 on: August 31, 2016, 12:29:39 pm »
Code: [Select]
] ulx nrslay ivis
1

[ERROR] addons/nrslay/lua/ulx/modules/autoban-slay.lua:18: attempt to compare number with boolean
  1. call - addons/nrslay/lua/ulx/modules/autoban-slay.lua:18
   2. __fn - addons/ulib/lua/ulib/shared/commands.lua:943
    3. unknown - addons/ulib/lua/ulib/shared/commands.lua:1296
     4. unknown - lua/includes/modules/concommand.lua:54


It prints out 1, as that's the default, I then did:

Code: [Select]
] ulx nrslay ivis 3
3

[ERROR] addons/nrslay/lua/ulx/modules/autoban-slay.lua:18: attempt to compare number with boolean
  1. call - addons/nrslay/lua/ulx/modules/autoban-slay.lua:18
   2. __fn - addons/ulib/lua/ulib/shared/commands.lua:943
    3. unknown - addons/ulib/lua/ulib/shared/commands.lua:1296
     4. unknown - lua/includes/modules/concommand.lua:54


and it print 3. I'm confused.
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: Nrslay, need some help
« Reply #9 on: August 31, 2016, 05:25:26 pm »
Code: Lua
  1. if not rounds >= 0 then
not rounds negates rounds before comparing it to 0. Yes, negating a string (a truthy value, which is false when negated) is possible in Lua.
You'll want parenthesis to indicate order of operations, or simply invert the expression. The inverse of >= is <, so if not rounds >= 0 becomes if rounds < 0.
bw81@ulysses-forums ~ % whoami
Homepage

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Nrslay, need some help
« Reply #10 on: August 31, 2016, 05:45:00 pm »
not rounds negates rounds before comparing it to 0. Yes, negating a string (a truthy value, which is false when negated) is possible in Lua.
You'll want parenthesis to indicate order of operations, or simply invert the expression. The inverse of >= is <, so if not rounds >= 0 becomes if rounds < 0.

That's what it's supposed to do. It's to check if someone puts a number below 0, it will error out, as it needs to be 0 or higher.

Code: Lua
  1. local banTime = 1440 -- Feel free to change this to any time. 0 = Permanent. In minutes
  2. --[[    
  3.                 1440 = 1 day
  4.                 10080 = 1 week
  5.                 https://www.google.com/#q=how%20many%20minutes%20in%201%20day
  6. ]]
  7.  
  8.  
  9. function ulx.nrslay( calling_ply, target_ply, rounds, reason )
  10.  
  11.  
  12.         print( rounds )
  13.  
  14.  
  15.         if reason == nil or "" then -- This is where the default reason goes. Change this to whatever you want.
  16.                
  17.                 reason = "RDM"
  18.  
  19.  
  20.         end
  21.  
  22.  
  23.         if rounds < 0 then
  24.                
  25.                 ULib.tsayError( calling_ply, "Invalid number of rounds. Must be 0 or higher." )
  26.  
  27.  
  28.         elseif rounds == 0 then
  29.        
  30.                 target_ply:RemovePData( "slays_left" )
  31.                 ulx.fancyLogAdmin( "#A has removed all slays from #T.", calling_ply, target_ply )
  32.  
  33.  
  34.         elseif rounds == 1 then
  35.                
  36.                 target_ply:SetPData( "slays_left", 1 )
  37.                 ulx.fancyLogAdmin( "#A will slay #T next round for #s.", calling_ply, target_ply, reason )
  38.  
  39.  
  40.         elseif rounds > 0 then
  41.                
  42.                 target_ply:SetPData( "slays_left", rounds )
  43.                 ulx.fancyLogAdmin( "#A will slay #T for the next #i rounds for #s.", calling_ply, target_ply, rounds, reason )
  44.  
  45.  
  46.         else -- Just to catch something that isn't specified.
  47.  
  48.  
  49.                 ULib.tsayError( calling_ply, "Unexpected error. Make sure you specified number of rounds." )
  50.  
  51.  
  52.         end
  53.  
  54.  
  55.         hook.Add( "PlayerDisconnected", "Warn Admins on slay leave/Auto ban on leave", function( ply )
  56.  
  57.  
  58.                 if ply:GetPData( "slays_left" ) >= 1 then
  59.                        
  60.                         ulx.fancyLogAdmin( ply:Nick() .. " has left with " .. tonumber( ply:GetPData( "slays_left" ) ) .. " slays left. They have been banned for (" ..  banTime .. ") minutes.")
  61.                         ULib.addBan( ply:SteamID(), banTime, "Leave with " .. tonumber( ply:GetPData( "slays_left" ) ) .. " slays left", ply:Nick(), "CONSOLE" ) -- This is what the ban will say on the menu's 'bans' menu, and in server logs. "CONSOLE" is the "admin" that banned them. Best to leave it console.
  62.  
  63.  
  64.                 end
  65.  
  66.  
  67.         end )
  68.  
  69.  
  70.         hook.Add( "PlayerAuthed", "Tell player how many slays they have left", function( ply, steamid, uniqueid )
  71.  
  72.  
  73.                 if not ply:GetPData( "slays_left" ) == nil or 0 then
  74.                        
  75.                         ULib.tsayError( ply, "You have " .. ply:GetPData( "slays_left" ) .. " slays left." )
  76.  
  77.  
  78.                 end
  79.  
  80.  
  81.         end )
  82.  
  83.  
  84.         hook.Add( "TTTBeginRound", "Actually Slay", function()
  85.  
  86.  
  87.                 for k,v in pairs( player.GetAll() ) do
  88.  
  89.  
  90.                         if v:GetPData( "slays_left" ) >= 1 then
  91.                                
  92.                                 v:Kill()
  93.                                 if IsValid( v.server_ragdoll ) then
  94.  
  95.  
  96.                                         local ply = player.GetByUniqueID( v.server_ragdoll.uqid )
  97.                                         if not IsValid( ply ) then return end
  98.                                         ply:SetNWBool( "body_found", true )
  99.                                         CORPSE.SetFound( v.server_ragdoll, true )
  100.                                         v.server_ragdoll:Remove()
  101.  
  102.  
  103.                                 end
  104.                                 v:SetPData( "slays_left", tonumber( v:GetPData( "slays_left" ) ) - 1 )
  105.                                 ulx.fancyLogAdmin( "#T was slain by #A for #s.", target_ply, calling_ply, reason )
  106.  
  107.  
  108.                         end
  109.  
  110.  
  111.                 end
  112.  
  113.  
  114.         end )
  115.  
  116.  
  117. end
  118. local nrslay = ulx.command( "TTT Admin", "ulx nrslay", ulx.nrslay, "!nrslay" )
  119. nrslay:addParam{ type=ULib.cmds.PlayerArg }
  120. nrslay:addParam{ type=ULib.cmds.NumArg, hint="rounds", ULib.cmds.round, ULib.cmds.optional, default = 1, min = 0 }
  121. nrslay:addParam{ type=ULib.cmds.StringArg, hint="reason", ULib.cmds.optional, ULib.cmds.takeRestOfLine }nrslay:defaultAccess( ULib.ACCESS_OPERATOR )

this is what I have now, and when I execute the command I get this
Code: [Select]
] ulx nrslay vis
1

[ERROR] addons/ulx/lua/ulx/log.lua:450: attempt to call method 'gsub' (a nil value)
  1. fancyLogAdmin - addons/ulx/lua/ulx/log.lua:450
   2. call - addons/nrslay/lua/ulx/modules/autoban-slay.lua:30
    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

What is gsub?
« Last Edit: August 31, 2016, 05:53:37 pm by iViscosity »
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: Nrslay, need some help
« Reply #11 on: September 01, 2016, 05:46:31 pm »
That's what it's supposed to do. It's to check if someone puts a number below 0, it will error out, as it needs to be 0 or higher.
Right, but it isn't doing that. It's not checking if it's less than zero, it's negating the variable rounds, THEN testing if it's greater than or equal to zero.

As for the gsub error, that's a function in the string library. You're likely passing a nil string or object or something somewhere.
bw81@ulysses-forums ~ % whoami
Homepage

  • Print