• Print

Author Topic: Entity:SetGravity()  (Read 6485 times)

0 Members and 1 Guest are viewing this topic.

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Entity:SetGravity()
« on: October 31, 2015, 06:55:20 pm »
Probably many of you answering these questions have answered one of mine before, and heres another one! (I really like experimenting). After playing on a server I saw that an Admin was able to set the gravity of a single person to a value (sbox_gravity x, but for players, thats for global). I found Entity:SetGravity(), am I able to replace Entity with something that would make the target_ply be affected? This is what I have so far. I get the error:
[ERROR] addons/ulx/lua/ulx/modules/sh/plygrav.lua:4: attempt to index local 'target_ply' (a nil value)
1. call - addons/ulx/lua/ulx/modules/sh/plygrav.lua:4
2. __fn - addons/ulib/lua/ulib/shared/commands.lua:942
3. unknown - addons/ulib/lua/ulib/shared/commands.lua:1295
4. unknown - lua/includes/modules/concommand.lua:54

Code: Lua
  1. local CATEGORY_NAME = "Advanced Simplicity"
  2.  
  3. function ulx.gravity( calling_ply, target_ply, string )
  4.         target_ply:SetGravity( "" .. string .. "" )
  5.         ulx.fancyLogAdmin( calling_ply, "#A has the set gravity of #T to #s", target_ply, string )
  6. end
  7. local gravity = ulx.command( CATEGORY_NAME, "ulx gravity", ulx.gravity, "!gravity" )
  8. gravity:defaultAcess( ULib.ACCESS_ADMIN )
  9. gravity:help( "Changes the value of a players gravity." )
  10.  

Also, since I am getting errors and when I do !gravity in chat it says I dont have permissions the errors fixed should show up when I try to add permissions in the groups tab under superadmin.
« Last Edit: October 31, 2015, 06:57:35 pm by WispySkies »

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Entity:SetGravity()
« Reply #1 on: October 31, 2015, 06:58:55 pm »
The SetGravity function takes a number, not a string.
Also, you never added arguments to your command. You need to tell it to take a target and a number (Around line 8)

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Entity:SetGravity()
« Reply #2 on: October 31, 2015, 07:15:27 pm »
The SetGravity function takes a number, not a string.
Also, you never added arguments to your command. You need to tell it to take a target and a number (Around line 8)
Had to separate that 8 and the ). Didnt know what you meant, parameters, great! One ulx thing I dont really know. Im using string to use the number I put in when doing !gravity NAME NUMBER so the 3rd part, string, gets put in Entity:SetGravity(). Could you do something like (change the string to number):
Code: Lua
  1. local CATEGORY_NAME = "Advanced Simplicity"
  2.  
  3. function ulx.gravity( calling_ply, target_ply, number )
  4.         target_ply:SetGravity( number )
  5.         ulx.fancyLogAdmin( calling_ply, "#A has the set gravity of #T to #s", target_ply, number )
  6. end
  7. local gravity = ulx.command( CATEGORY_NAME, "ulx gravity", ulx.gravity, "!gravity" ) -- Insert addParam below
  8. gravity:defaultAccess( ULib.ACCESS_ADMIN )
  9. gravity:help( "Changes the value of a players gravity." )
  10.  

Does that fix the target_ply issue when finished, and the command not showing up when I want to add it to a user.

EDIT: I just realized I did gravity:defaultAccess. That fixes the command not showing up issue.
« Last Edit: October 31, 2015, 07:17:25 pm by WispySkies »

Offline monkeymacman

  • Newbie
  • *
  • Posts: 44
  • Karma: 13
Re: Entity:SetGravity()
« Reply #3 on: October 31, 2015, 08:00:25 pm »
I think you can convert a string/variable to a number by using tonumber(variable) so for example you could do target_ply:SetGravity(tonumber(string)) or before that do string = tonumber(string) or something like that. And then for the param just have gravity:addParam{ type=ULib.cmds.StringArg, hint="gravity" } also I'm not sure if you even added a player arg for the target_ply so I think you would do something like
Code: Lua
  1. local CATEGORY_NAME = "Advanced Simplicity"
  2.  
  3. function ulx.gravity( calling_ply, target_ply, string )
  4.         string = tonumber( string )
  5.         target_ply:SetGravity( string )
  6.         ulx.fancyLogAdmin( calling_ply, "#A has the set gravity of #T to #s", target_ply, string )
  7. end
  8. local gravity = ulx.command( CATEGORY_NAME, "ulx gravity", ulx.gravity, "!gravity" )
  9. gravity:addParam{ type=ULib.cmds.PlayersArg }
  10. gravity:addParam{ type=ULib.cmds.StringArg, hint="gravity" }
  11. gravity:defaultAcess( ULib.ACCESS_ADMIN )
  12. gravity:help( "Changes the value of a players gravity." )
Might not work though, or at least not work perfectly. Not sure about using number args, but there is probably a better way to do it using that. maybe something like this:
Code: Lua
  1. local CATEGORY_NAME = "Advanced Simplicity"
  2.  
  3. function ulx.gravity( calling_ply, target_ply, grav )
  4.         target_ply:SetGravity( grav )
  5.         ulx.fancyLogAdmin( calling_ply, "#A has the set gravity of #T to #s", target_ply, grav )
  6. end
  7. local gravity = ulx.command( CATEGORY_NAME, "ulx gravity", ulx.gravity, "!gravity" )
  8. gravity:addParam{ type=ULib.cmds.PlayersArg }
  9. gravity:addParam{ type=ULib.cmds.NumArg, default=600, hint="gravity number", min=0, max=2000 }
  10. gravity:defaultAcess( ULib.ACCESS_ADMIN )
  11. gravity:help( "Changes the value of a players gravity." )
But I'm not sure.
« Last Edit: October 31, 2015, 08:02:35 pm by monkeymacman »

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Entity:SetGravity()
« Reply #4 on: November 01, 2015, 04:11:27 am »
Might not work though, or at least not work perfectly. Not sure about using number args, but there is probably a better way to do it using that. maybe something like this:
Code: Lua
  1. [code=lua]local CATEGORY_NAME = "Advanced Simplicity"
  2.  
  3. function ulx.gravity( calling_ply, target_ply, grav )
  4.         target_ply:SetGravity( grav )
  5.         ulx.fancyLogAdmin( calling_ply, "#A has the set gravity of #T to #s", target_ply, grav )
  6. end
  7. local gravity = ulx.command( CATEGORY_NAME, "ulx gravity", ulx.gravity, "!gravity" )
  8. gravity:addParam{ type=ULib.cmds.PlayersArg }
  9. gravity:addParam{ type=ULib.cmds.NumArg, default=600, hint="gravity number", min=0, max=2000 }
  10. gravity:defaultAcess( ULib.ACCESS_ADMIN )
  11. gravity:help( "Changes the value of a players gravity." )
But I'm not sure.
I think that one will work better, thanks!
« Last Edit: November 01, 2015, 04:19:44 am by WispySkies »

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Entity:SetGravity()
« Reply #5 on: November 01, 2015, 04:55:45 am »
No error, gravity just does not set.  :-[

Offline monkeymacman

  • Newbie
  • *
  • Posts: 44
  • Karma: 13
Re: Entity:SetGravity()
« Reply #6 on: November 01, 2015, 05:33:05 am »
Oh, wait, I just realized Access is misspelled in all the code on this thread *facepalm* all the codes here have only one c, it should be defaultAccess not defaultAcess. I doubt that's the problem but it's noteworthy.
« Last Edit: November 01, 2015, 06:08:13 am by monkeymacman »

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Entity:SetGravity()
« Reply #7 on: November 01, 2015, 05:46:41 am »
Oh, wait, I just realized Access is misspelled in all the code on this thread *facepalm* all the codes here have only one c, it should be defaultAccess no defaultAcess. I doubt that's the problem but it's noteworthy.
Yeah I noticed that LOL. Forget to fix it in what I had posted. Any thought on why its not updating the gravity?

Offline monkeymacman

  • Newbie
  • *
  • Posts: 44
  • Karma: 13
Re: Entity:SetGravity()
« Reply #8 on: November 01, 2015, 06:00:07 am »
Okay, I was able to get it to work, apparently it has to be using a for loop for whatever reason. I also realized that for SetGravity() 1 is normal gravity, also setting it to 0 does nothing so you could use something like this...
Code: Lua
  1. CATEGORY_NAME = "Advanced Simplicty"
  2. function ulx.gravity( calling_ply, target_ply, grav )
  3.     gravnum = grav /600
  4.     for k,v in pairs( target_ply ) do
  5.         if grav == 0 then
  6.             v:SetGravity( 0.0001 )
  7.         else
  8.             v:SetGravity( gravnum )
  9.         end
  10.     end
  11.     ulx.fancyLogAdmin( calling_ply, "#A has the set gravity of #T to #s", target_ply, grav )
  12. end
  13.  
  14. local gravity = ulx.command( CATEGORY_NAME, "ulx gravity", ulx.gravity, "!gravity" )
  15. gravity:addParam{ type=ULib.cmds.PlayersArg }
  16. gravity:addParam{ type=ULib.cmds.NumArg, default=600, hint="Gravity Number", min=0, max=1000 }
  17. gravity:defaultAccess( ULib.ACCESS_ADMIN )
  18. gravity:help( "Changes the value of a players gravity. Normal value is 600." )
This worked for me, so it should work for you. Also, this way if you set it to 0 then when you are in the air it is like you just keep going whichever way you were, so you go to the sky basically forever when you jump, and if you walk off the cliff you just float there.
« Last Edit: November 01, 2015, 06:10:29 am by monkeymacman »

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Entity:SetGravity()
« Reply #9 on: November 01, 2015, 07:04:29 am »
It works, thank you.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Entity:SetGravity()
« Reply #10 on: November 01, 2015, 07:16:56 am »
Figured I'd mention this even though the command was finished:
You need a for loop in your code b/c you added PlayersArg rather than PlayerArg as a parameter
If you remove the s then you can remove the for loop but only run the command on one player at a time

Offline monkeymacman

  • Newbie
  • *
  • Posts: 44
  • Karma: 13
Re: Entity:SetGravity()
« Reply #11 on: November 01, 2015, 07:19:59 am »
Figured I'd mention this even though the command was finished:
You need a for loop in your code b/c you added PlayersArg rather than PlayerArg as a parameter
If you remove the s then you can remove the for loop but only run the command on one player at a time
Oh yeah, that makes sense. I always forget that you have to loop through players if there is multiple of them. Thanks for the clarification.

  • Print