• Print

Author Topic: Nil  (Read 7662 times)

0 Members and 1 Guest are viewing this topic.

Offline dankpepe

  • Jr. Member
  • **
  • Posts: 51
  • Karma: 1
Nil
« on: May 17, 2016, 05:54:25 pm »
Code: Lua
  1. local CATEGORY_NAME = "undercover"
  2.  
  3.  
  4. function ulx.undercover( calling_ply, name, should_revoke )
  5.    
  6.        
  7.        
  8.         id = calling_ply:SteamID()
  9.         ULib.ucl.userAllow(id,"ulx uncover",false)
  10.  
  11.     if not should_revoke then
  12.         local ply_name = calling_ply:GetName()
  13.         local rank = calling_ply:GetUserGroup()
  14.         calling_ply:setRPName(name)
  15.         calling_ply:SetUserGroup("user")
  16.  
  17.         else
  18.         calling_ply:setRPName(ply_name)
  19.          calling_ply:SetUserGroup(tostring(rank))
  20.         print(ply_name)
  21.         print(rank)
  22.         print("works")
  23.         end
  24.  
  25. end
  26.  
  27.  
  28. local undercover = ulx.command( CATEGORY_NAME, "ulx undercover", ulx.undercover, "!undercover", true )
  29. undercover:addParam{ type=ULib.cmds.StringArg, hint="Name to change", ULib.cmds.takeRestOfLine }
  30. undercover:addParam{ type=ULib.cmds.BoolArg, invisible=true }
  31. undercover:defaultAccess( ULib.ACCESS_ADMIN )
  32. undercover:help( "Makes you go undercover" )
  33. undercover:setOpposite( "ulx uncover", { _, _, true }, "!uncover" )
  34.  
  35.  
  36.  
  37.  

When I run this without the tostring() in line 19, it returns an error saying expected string. When I change it to string, it becomes nil. I have printed the vars in the first section, and they were fine. When they get printed the second time, when it is revoked, they both come back nil.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Nil
« Reply #1 on: May 17, 2016, 06:09:24 pm »
If your code was properly indented, you could tell what was going on easier.

Code: Lua
  1. local CATEGORY_NAME = "undercover"
  2.  
  3. function ulx.undercover( calling_ply, name, should_revoke )
  4.     id = calling_ply:SteamID()
  5.     ULib.ucl.userAllow(id,"ulx uncover",false)
  6.  
  7.     if not should_revoke then
  8.         local ply_name = calling_ply:GetName()
  9.         local rank = calling_ply:GetUserGroup()
  10.         calling_ply:setRPName(name)
  11.         calling_ply:SetUserGroup("user")
  12.     else
  13.         calling_ply:setRPName(ply_name)
  14.         calling_ply:SetUserGroup(tostring(rank))
  15.         print(ply_name)
  16.         print(rank)
  17.         print("works")
  18.     end
  19. end
  20.  
  21. local undercover = ulx.command( CATEGORY_NAME, "ulx undercover", ulx.undercover, "!undercover", true )
  22. undercover:addParam{ type=ULib.cmds.StringArg, hint="Name to change", ULib.cmds.takeRestOfLine }
  23. undercover:addParam{ type=ULib.cmds.BoolArg, invisible=true }
  24. undercover:defaultAccess( ULib.ACCESS_ADMIN )
  25. undercover:help( "Makes you go undercover" )
  26. undercover:setOpposite( "ulx uncover", { _, _, true }, "!uncover" )

In your code, rank is declared as a local variable in the scope if not should_revoke then. However, you attempt to access it in the else clause in that statement. Same goes for ply_name.
To remedy this, move the variables to a higher scope (ie the main function body).

edit: There is also a pre-made "undercover" script here on the forums. You may want to modify that to suit your needs instead of writing your own.
« Last Edit: May 17, 2016, 06:11:00 pm by Bytewave »
bw81@ulysses-forums ~ % whoami
Homepage

Offline dankpepe

  • Jr. Member
  • **
  • Posts: 51
  • Karma: 1
Re: Nil
« Reply #2 on: May 17, 2016, 08:37:49 pm »
When I put the var in the body of the script it triggers after the first command and sets what my new name is to the var, making it pointless. Do you have any ideas how I can fix this?

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Nil
« Reply #3 on: May 18, 2016, 04:51:04 am »
When I put the var in the body of the script it triggers after the first command and sets what my new name is to the var, making it pointless. Do you have any ideas how I can fix this?

I'm not sure exactly what your problem is, but I think you could solve it by simply declaring the variables in the function scope, and then assigning them a value in the if statement.

Code: Lua
  1. function myFunction( myArgument )
  2.   local myLocalVar
  3.   if condition then
  4.     myLocalVar = "someValue"
  5.   end
  6. end
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 dankpepe

  • Jr. Member
  • **
  • Posts: 51
  • Karma: 1
Re: Nil
« Reply #4 on: May 18, 2016, 06:58:09 am »
I tried that, but it's still returning nil.

Code: Lua
  1. local CATEGORY_NAME = "undercover"
  2.  
  3.  
  4. function ulx.undercover( calling_ply, name, should_revoke )
  5.    
  6.        
  7.    
  8.     local ply_name
  9.     local rank
  10.     if not should_revoke then
  11.         ply_name = calling_ply:GetName()
  12.         rank = calling_ply:GetUserGroup()
  13.     end
  14.  
  15.     local id = calling_ply:SteamID()   
  16.         ULib.ucl.userAllow(id,"ulx uncover",false)
  17.  
  18.     if should_revoke then
  19.         --calling_ply:setRPName(name)
  20.         calling_ply:SetUserGroup(tostring(rank))
  21.         print(rank)
  22.         else
  23.         --calling_ply:setRPName()
  24.         calling_ply:SetUserGroup("user")
  25.         print(rank)
  26.         end
  27.  
  28. end
  29.  
  30.  
  31. local undercover = ulx.command( CATEGORY_NAME, "ulx undercover", ulx.undercover, "!undercover", true )
  32. undercover:addParam{ type=ULib.cmds.StringArg, hint="Name to change", ULib.cmds.takeRestOfLine }
  33. undercover:addParam{ type=ULib.cmds.BoolArg, invisible=true }
  34. undercover:defaultAccess( ULib.ACCESS_ADMIN )
  35. undercover:help( "Makes you go undercover" )
  36. undercover:setOpposite( "ulx uncover", { _, _, true }, "!uncover" )
  37.  
  38.  


Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Nil
« Reply #5 on: May 18, 2016, 08:46:55 am »
You're not returning anything in that function. What is nil?
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 dankpepe

  • Jr. Member
  • **
  • Posts: 51
  • Karma: 1
Re: Nil
« Reply #6 on: May 18, 2016, 09:36:27 am »
The ply_name and the rank variable are nil.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Nil
« Reply #7 on: May 18, 2016, 09:43:26 am »
You're setting the variables when should_revoke is false, but working with them when should_revoke is true. They're nil because you don't give them a value when you work with them, but do give them a value when you don't work with them.
« Last Edit: May 18, 2016, 09:45:25 am by Bytewave »
bw81@ulysses-forums ~ % whoami
Homepage

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Nil
« Reply #8 on: May 18, 2016, 11:25:19 am »
Instead of doing "local <variable> = <target>", instead just do "<variable> = <target>". To clarify what they meant, if you don't get it, is you're defining your two variables only when in the "if not should_revoke" which means you can't use them in the "if should_revoke" (else) as they aren't set to anything anymore and return nil.

Also, it may help if you do "elseif should_revoke" so that everything stays in one. Even though you are defining the ply_name and rank in the function, you're not setting them to anything.
« Last Edit: May 18, 2016, 11:27:44 am by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline dankpepe

  • Jr. Member
  • **
  • Posts: 51
  • Karma: 1
Re: Nil
« Reply #9 on: May 18, 2016, 11:40:56 am »
Thank you so much, getting rid of local fixed it. Do you know if there is anyway I can give a certain user all the permissions from a group?

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Nil
« Reply #10 on: May 18, 2016, 12:56:38 pm »
Using a global variable is 99.9% not the correct way of doing this. Unless you absolutely need to, you should avoid using global variables.

Why are you only assigning ply_name and rank when should_revoke is true? If you're only going to use it in the second if statement, just move the variable assignment into that if statement.
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 OpticPotatOS

  • Jr. Member
  • **
  • Posts: 53
  • Karma: 0
  • GLaDOS
    • Gaming for Everyone
Re: Nil
« Reply #11 on: May 19, 2016, 10:53:50 am »
Thank you so much, getting rid of local fixed it. Do you know if there is anyway I can give a certain user all the permissions from a group?

Make a group that inherits from your preferred group, or use ulx userallow on the player for all the commands necessary.
My website isn't my website, rather, it's the gaming network my sever is connected to.

ttt.gfe.nu

Unforeseen Consequences

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Nil
« Reply #12 on: May 19, 2016, 12:59:22 pm »
Thank you so much, getting rid of local fixed it. Do you know if there is anyway I can give a certain user all the permissions from a group?

"ulx addgroup <group> <inherit>". The "inherit" will make it so that the new "group" that you are making will have everything in the group that it's inheriting from, plus whatever you add onto it.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline dankpepe

  • Jr. Member
  • **
  • Posts: 51
  • Karma: 1
Re: Nil
« Reply #13 on: May 19, 2016, 02:43:14 pm »
I'm looking to give a user permissions, not create a whole group. There is ucl.userAllow(id,access,revoke,deny) where you can set access to a table of values. I was wondering if there was a way to easily find/make a table of permissions a group has, so I can add those to a person.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Nil
« Reply #14 on: May 19, 2016, 02:44:37 pm »
I don't know if it's in the config files anywhere, but there is a command "ulx userallow" or "ulx userallowid" but I'm not familiar with any lists of permissons, sorry.

You can use those two in-game.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

  • Print