• Print

Author Topic: my lua script won't work  (Read 5983 times)

0 Members and 1 Guest are viewing this topic.

Offline Patrick Palson

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
my lua script won't work
« on: December 21, 2016, 06:15:00 pm »
Code: Lua
  1. --This is where your immunity is.
  2. --Example: immunity1 cannot interact with immunity2 and up.
  3.  
  4. --Supports 20 immunity ranks. Shouldn't need anymore than that!
  5.  
  6. local immunity2 = {"admin"}
  7. local immunity5 = {"Developer"}
  8.  
  9. function PlayerPickedUpProp( ply, ent )
  10.  
  11. if table.HasValue((immunity5), ply:GetNWString("usergroup")) and ent:GetOwner(table.HasValue((immunity5), ply:GetNWString("usergroup"))) then return true end
  12.  
  13. if table.HasValue((immunity2), ply:GetNWString("usergroup")) and ent:GetOwner(table.HasValue((immunity5), ply:GetNWString("usergroup"))) then return false end
  14.  
  15. if table.HasValue((immunity2), ply:GetNWString("usergroup")) and ent:GetOwner(table.HasValue((immunity2), ply:GetNWString("usergroup"))) then return true end
  16.  
  17. if table.HasValue((immunity5), ply:GetNWString("usergroup")) and ent:GetOwner(table.HasValue((immunity2), ply:GetNWString("usergroup"))) then return false end
  18. end
  19. hook.Add( "PhysgunPickup", "PlayerPickedUpProp", PlayerPickedUpProp )
  20.  
  21.  

I basically want Developer to not pick up admin's props and admin not to pick up Developer's props just to test and see if it works right.
So, right now Developer can pick up all props, admin can't pick up any props, I am not sure what's going on PLEASE HELP!

Note: I want to keep tables, because this will be an addon soon and people will be able to configure it really easily by just changing the immunity table names.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: my lua script won't work
« Reply #1 on: December 21, 2016, 07:20:24 pm »
I'm not incredibly knowledgeable about the whole prop targeting, but it MAY help if you use "ply:GetUserGroup()" instead of "ply:GetNWString()".

Sent using Tapatalk. Owner of iViscosity Gaming.

I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline Patrick Palson

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
Re: my lua script won't work
« Reply #2 on: December 21, 2016, 08:42:57 pm »
Code: Lua
  1. if table.HasValue((immunity5), ply:GetUserGroup()) and ent:GetOwner(table.HasValue((immunity5), ply:GetNWString("usergroup"))) then return true end
Made no difference, one thing I noticed though, is that the script IS working, but it only is reading the first line, like the first line is
Code: Lua
  1. if table.HasValue((immunity5), ply:GetUserGroup()) and ent:GetOwner(table.HasValue((immunity5), ply:GetNWString("usergroup"))) then return true end
and if I put return true to return false, then the Developer can no longer pick up props, but the other lines aren't taking effect for some reason.

I believe it has something to do with
Code: Javascript
  1. return false end
  and
Code: Javascript
  1. return true end

at the ending of each line, is there anyway I can put all the ends below all of the lines instead of ending each line separately?
« Last Edit: December 21, 2016, 08:51:14 pm by Patrick Palson »

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: my lua script won't work
« Reply #3 on: December 21, 2016, 09:07:21 pm »
That's the way functions in lua (and most languages) work.
Once you return, value or not, it won't go further.
You could use logical operators, and, or, not, and perhaps an else.
Plenty of lua tutorials out on web for how to do that. (and, or, not, if else)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Patrick Palson

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
Re: my lua script won't work
« Reply #4 on: December 22, 2016, 12:01:12 am »
Anybody able to help?

Offline Patrick Palson

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
Re: my lua script won't work
« Reply #5 on: December 22, 2016, 12:09:04 am »
Code: Lua
  1. function PlayerPickedUpProp( ply, ent )
  2. if table.HasValue((immunity5), ply:GetUserGroup()) and ent:GetOwner(table.HasValue((immunity5), ply:GetNWString("usergroup"))) then pickup = true
  3. if table.HasValue((immunity5), ply:GetUserGroup()) and ent:GetOwner(table.HasValue((immunity2), ply:GetNWString("usergroup"))) then pickup = false
  4. if table.HasValue((immunity2), ply:GetUserGroup()) and ent:GetOwner(table.HasValue((immunity2), ply:GetNWString("usergroup"))) then pickup = true
  5. if pickup == true then return true end
  6. if pickup == false then return false end
  7. end
  8. end
  9. end
  10. end
  11. hook.Add( "PhysgunPickup", "PlayerPickedUpProp", PlayerPickedUpProp )

Trying some new things, nothing works, I'll keep trying.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: my lua script won't work
« Reply #6 on: December 22, 2016, 12:34:25 am »
How does your script know whose props are whose?

If you are using some kind of prop protection mod, you'd need to tie that into this. If not, you'll need to track props and entity spawns and assign owners so that you can determine who can pick them up.

Offline Patrick Palson

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
Re: my lua script won't work
« Reply #7 on: December 22, 2016, 12:42:04 am »
It finds the owner of the entity being picked up, and that person's rank.
I've had it working before where only I could pick up the admin's prop and the admin couldn't pick up my prop.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: my lua script won't work
« Reply #8 on: December 23, 2016, 01:08:52 pm »
Code: Lua
  1. function PlayerPickedUpProp( ply, ent )
  2. if table.HasValue((immunity5), ply:GetUserGroup()) and ent:GetOwner(table.HasValue((immunity5), ply:GetNWString("usergroup"))) then pickup = true
  3. if table.HasValue((immunity5), ply:GetUserGroup()) and ent:GetOwner(table.HasValue((immunity2), ply:GetNWString("usergroup"))) then pickup = false
  4. if table.HasValue((immunity2), ply:GetUserGroup()) and ent:GetOwner(table.HasValue((immunity2), ply:GetNWString("usergroup"))) then pickup = true
  5. if pickup == true then return true end
  6. if pickup == false then return false end
  7. end
  8. end
  9. end
  10. end
  11. hook.Add( "PhysgunPickup", "PlayerPickedUpProp", PlayerPickedUpProp )

Trying some new things, nothing works, I'll keep trying.

I'm not sure if it makes a different, but Entity:GetOwner doesn't take any arguments. If you're trying to see if the owner of the prop has one of the ranks in immunity5, for example, you need to do the following:

Code: Lua
  1. local owner = ent:GetOwner()
  2. if table.HasValue(immunity5, ply:GetUserGroup()) and table.HasValue(immunity5, owner:GetUserGroup()) then pickup = true end

(To those more experienced with Lua: does Lua support function chaining? I've been working a lot with JavaScript lately so I originally wrote this example with ent:GetOwner():GetUserGroup(), but I'm not 100% stuff like that works in Lua.)



Another thing is that your code will only return something if it gets through all three if statements, which will never happen (assuming immunity5 and immunity2 have the same value as they did in your first post). You could see this pretty easily if you indented your code, like so:

Code: Lua
  1. function PlayerPickedUpProp( ply, ent )
  2.   if table.HasValue((immunity5), ply:GetUserGroup()) and ent:GetOwner(table.HasValue((immunity5), ply:GetNWString("usergroup"))) then pickup = true
  3.     if table.HasValue((immunity5), ply:GetUserGroup()) and ent:GetOwner(table.HasValue((immunity2), ply:GetNWString("usergroup"))) then pickup = false
  4.       if table.HasValue((immunity2), ply:GetUserGroup()) and ent:GetOwner(table.HasValue((immunity2), ply:GetNWString("usergroup"))) then pickup = true
  5.         if pickup == true then return true end
  6.         if pickup == false then return false end
  7.       end
  8.     end
  9.   end
  10. end
  11. hook.Add( "PhysgunPickup", "PlayerPickedUpProp", PlayerPickedUpProp )

I use two spaces to indent, but you can use whatever works for you. 4 spaces, tabs, etc.

To make sure the final if statements are reached, move them outside of the other if statements like so:

Code: Lua
  1. function PlayerPickedUpProp( ply, ent )
  2.   if table.HasValue((immunity5), ply:GetUserGroup()) and ent:GetOwner(table.HasValue((immunity5), ply:GetNWString("usergroup"))) then pickup = true
  3.     if table.HasValue((immunity5), ply:GetUserGroup()) and ent:GetOwner(table.HasValue((immunity2), ply:GetNWString("usergroup"))) then pickup = false
  4.       if table.HasValue((immunity2), ply:GetUserGroup()) and ent:GetOwner(table.HasValue((immunity2), ply:GetNWString("usergroup"))) then pickup = true end
  5.     end
  6.   end
  7.   if pickup == true then return true end
  8.   if pickup == false then return false end
  9. end
  10. hook.Add( "PhysgunPickup", "PlayerPickedUpProp", PlayerPickedUpProp )

Taking this a step further, you can simplify those if statements to a simple return call. Because pickup is either true or false, you don't need to check it's value and return the Boolean literal; you can just return pickup:

Code: Lua
  1. function PlayerPickedUpProp( ply, ent )
  2.   if table.HasValue((immunity5), ply:GetUserGroup()) and ent:GetOwner(table.HasValue((immunity5), ply:GetNWString("usergroup"))) then pickup = true
  3.     if table.HasValue((immunity5), ply:GetUserGroup()) and ent:GetOwner(table.HasValue((immunity2), ply:GetNWString("usergroup"))) then pickup = false
  4.       if table.HasValue((immunity2), ply:GetUserGroup()) and ent:GetOwner(table.HasValue((immunity2), ply:GetNWString("usergroup"))) then pickup = true end
  5.     end
  6.   end
  7.   return pickup
  8. end
  9. hook.Add( "PhysgunPickup", "PlayerPickedUpProp", PlayerPickedUpProp )

This doesn't fix the fact that the inner if statements are useless. If you just want it to block the pickup up of props belonging to players who outrank you, the following code should accomplish the same thing with less code (not that less code is always good):

Code: Lua
  1. function PlayerPickedUpProp( ply, ent )
  2.   if table.HasValue((immunity5), ply:GetUserGroup()) and ent:GetOwner(table.HasValue((immunity2), ply:GetNWString("usergroup"))) then return false end
  3.   return true
  4. end
  5. hook.Add( "PhysgunPickup", "PlayerPickedUpProp", PlayerPickedUpProp )

This will return false if the player is outranked by the owner of the prop, and true otherwise.
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

  • Print