• Print

Author Topic: attempt to index a string value with bad key  (Read 9651 times)

0 Members and 1 Guest are viewing this topic.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
attempt to index a string value with bad key
« on: November 01, 2016, 01:50:21 pm »
I'm really confused as to why this is throwing this error... Idk if I'm just tired and can't see why, but can you help me see?

Code: Lua
  1.  
  2. local ttt = GetConVar( "gamemode" ):GetString() == "terrortown"
  3.  
  4.  
  5. function ulx.softban( calling_ply, target_ply, should_ban )
  6.  
  7.  
  8.    local weps = {}
  9.  
  10.  
  11.    if should_ban then
  12.      
  13.       target_ply:SetPData( "softbanned", "true" )
  14.       target_ply:Freeze( true )
  15.       ulx.fancyLogAdmin( calling_ply, "#A softbanned #T", target_ply )
  16.  
  17.  
  18.       for k,v in pairs( target_ply:GetWeapons() ) do
  19.          
  20.          table.insert( weps, k )
  21.  
  22.  
  23.       end
  24.      
  25.       target_ply:StripWeapons()
  26.  
  27.  
  28.       if ttt then
  29.  
  30.  
  31.          if target_ply:GetRole() == ROLE_INNOCENT then return end -- No need to make an innocent innocent again.
  32.          target_ply:SetRole( ROLE_INNOCENT ) -- Just in-case.
  33.  
  34.  
  35.       end
  36.      
  37.    else
  38.      
  39.       target_ply:RemovePData( "softbanned" )
  40.       target_ply:Freeze( false )
  41.       target_ply:Give( weps )
  42.       ulx.fancyLogAdmin( calling_ply, "#A un-softbanned #T", target_ply )
  43.      
  44.    end
  45.    
  46. end
  47. local softban = ulx.command( "Utility", "ulx softban", ulx.softban, "!softban" ) -- This was the troublesome line, I had "ulx.softban" and not "ulx.command"
  48. softban:addParam{ type=ULib.cmds.PlayerArg }
  49. softban:addParam{ type=ULib.cmds.BoolArg, invisible=true }
  50. softban:setOpposite( "ulx unsoftban", { _, _, false }, "!unsoftban" )
  51. softban:defaultAccess( ULib.ACCESS_ADMIN )
  52. softban:help( "'Bans' a target without actually kicking them from the server" )
  53.  
  54.  
  55. local function DenyText( text, bTeam, listener, speaker )
  56.    
  57.    if speaker:GetPData( "softbanned" ) and speaker:GetPData( "softbanned" ) == "true" then
  58.      
  59.       return false
  60.      
  61.    end
  62.    
  63. end
  64. hook.Add( "PlayerCanSeePlayersChat", "DenyText", DenyText )
  65.  
  66.  
  67. local function DenySpeech( listener, talker )
  68.    
  69.    if talker:GetPData( "softbanned" ) == "true" then
  70.      
  71.       return false
  72.      
  73.    end
  74.    
  75. end
  76. hook.Add( "PlayerCanHearPlayersVoice", "DenySpeech", DenySpeech )
  77.  

Code: [Select]
[ERROR] addons/ulx-softban-master/lua/ulx/modules/sh/softban.lua:9: attempt to index a string value with bad key ('SetPData' is not part of the string library)
  1. error - [C]:-1
   2. __index - lua/includes/extensions/string.lua:301
    3. softban - addons/ulx-softban-master/lua/ulx/modules/sh/softban.lua:9
     4. unknown - addons/ulx-softban-master/lua/ulx/modules/sh/softban.lua:38
      5. include - [C]:-1
       6. unknown - addons/ulx/lua/ulx/cl_init.lua:17
        7. include - [C]:-1
         8. unknown - addons/ulx/lua/ulib/modules/ulx_init.lua:4
          9. include - [C]:-1
           10. unknown - addons/ulib/lua/ulib/cl_init.lua:24
            11. include - [C]:-1
             12. unknown - addons/ulib/lua/autorun/ulib_init.lua:5

« Last Edit: November 02, 2016, 07:37:57 am by iViscosity »
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: attempt to index a string value with bad key
« Reply #1 on: November 01, 2016, 06:41:40 pm »
Lua gives you this error because you're trying to use 'SetPData' on a string.
Code: Lua
  1. str = "Hello World"
  2. str:SetPData() -- Error: attempt to index a string value with bad key ('SetPData' is not part of the string library)

Something is calling the ulx.softban function with a string as the second argument.

Hint: line 47

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: attempt to index a string value with bad key
« Reply #2 on: November 01, 2016, 07:36:06 pm »
Lua gives you this error because you're trying to use 'SetPData' on a string.
Code: Lua
  1. str = "Hello World"
  2. str:SetPData() -- Error: attempt to index a string value with bad key ('SetPData' is not part of the string library)

Something is calling the ulx.softban function with a string as the second argument.

Hint: line 47
I can't believe I actually did that... That's the first time I've ever done that lmao thank you for pointing it out to me
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: attempt to index a string value with bad key
« Reply #3 on: November 01, 2016, 08:05:10 pm »
Sheesh.
I've looked at the bad code example 10 times and missed it.
I hang my head in shame as a team member.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: attempt to index a string value with bad key
« Reply #4 on: November 02, 2016, 02:48:29 am »
Sheesh.
I've looked at the bad code example 10 times and missed it.
I hang my head in shame as a team member.

Hah, I looked at it for about 2 or 3 hours and never saw it. Don't feel bad
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: attempt to index a string value with bad key
« Reply #5 on: November 02, 2016, 07:36:58 am »
On a related note, I have a question about the weapons logging I have in there (the table.insert( weps, k )). If I'm going to be storing the weapons a target has, should I be inserting the key or the value to give it back to them later with ply:Give()? Or is there another way and this was just won't work?
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: attempt to index a string value with bad key
« Reply #6 on: November 02, 2016, 03:06:27 pm »
Maybe I don't understand what you're doing.
Why not use Player:GetWeapons and store that table for use later to Give?
« Last Edit: November 02, 2016, 03:08:34 pm by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: attempt to index a string value with bad key
« Reply #7 on: November 02, 2016, 03:18:38 pm »
Code: Lua
  1. for k,v in pairs( target_ply:GetWeapons() ) do
  2.          
  3.          table.insert( weps, k )
  4.  
  5.  
  6.       end
From my code, what I'm wondering is how can I give them this back to them later? Is this right? Do I need to store k or v?

Basically, how can I store GetWeapons?


And OT: But how do you get the Player:GetWeapons to look like that? :P
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: attempt to index a string value with bad key
« Reply #8 on: November 02, 2016, 06:36:04 pm »
Getweapons is already a table, why are you looping through it to make another table?
My understanding of the wiki description without actually having had used/experience in this command may make me unreliable and incorrect, but, the wiki says it stores the table.
So you could simply use
Code: [Select]
local target_ply.weps = { target_ply:GetWeapons() }
(You're going to want to attach the weps table to each player who might be softbanned while on server, hence the target_ply.weps
You're also going to have to check/get the ammo counts for each weapon and store that to give to them later.

Now, the challenge, Give expects weapon ClassNames. You can't just loop back through and Give ( target_ply.weps[var] )
You'd have to loop through the table and do something like Give ( target_ply.weps[var]:GetClass() )
But, you also have to store weapon ammo. I'm not sure GetWeapons does that.

*click* Few minutes later.
Here, found this example that looks 98% accurate like it may work out of the box on Facepunch
https://facepunch.com/showthread.php?t=1477280&s=0258ce5b3e965249181345973d9c7220&p=48281605&viewfull=1#post48281605
(I know, I know, surprise surprise..it only took about 20 posts for someone to just feed the guy straight out after several posts explaining what might work, why it was a bad idea, the argument on why it wasn't a bad idea, then the apology by the person who stated they were wrong about it being a bad idea, then answering in straight code)
PlayerDeath hook grabs.
PlayerSay hook, about half way down, shows the general give code.
You should be able to figure out how to modify it for your needs, using our codestyle to match ULX.


"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: attempt to index a string value with bad key
« Reply #9 on: November 03, 2016, 07:40:48 am »
I'll try to guide you through my thoughts before I try that, basically I knew GetWeapons() returned a table, but I didn't know if the key returned the classname or if the value did, and I wanted some way to store it for later, because I knew I couldn't do Give( target_ply:GetWeapons() ) because at that point I've already stripped weapons. I'll take a look into that when I get home and try it out though.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

  • Print