• Print

Author Topic: Best way to decrease a value every x sec  (Read 4767 times)

0 Members and 1 Guest are viewing this topic.

Offline XxLMM13xX

  • Sr. Member
  • ****
  • Posts: 265
  • Karma: -51
  • New to lua development
    • Twitch
Best way to decrease a value every x sec
« on: June 14, 2015, 07:15:58 am »
Ok so im trying to make a battery system on a printer and i need a value to decrease by 1 every 13sec

local battery = 100

Thats all i got! I need help making this decrease on the printer HUD!

ex. 100 (3 sec later) 99 (3 more sec later) 98

I have NO idea how to do this!!! PLEASE HELP!

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Best way to decrease a value every x sec
« Reply #1 on: June 14, 2015, 08:43:02 am »
This is how I'd do it
http://wiki.garrysmod.com/page/timer/Create
See if you can find out how by using that function

Offline Tomzen

  • Full Member
  • ***
  • Posts: 115
  • Karma: -1
  • A new lua adventurer
    • Thirdage Gaming
Re: Best way to decrease a value every x sec
« Reply #2 on: June 14, 2015, 07:28:44 pm »
Yeah, probably late, but:

Code: Lua
  1. timer.Create( "BatteryDecreaser", 3, 1, function() battery = battery - 1 end )

I think. my timer is rusty, haven't used it in a while.
The first part is the name (BatteryDecreaser), then how many seconds you want it to wait (1) and then how many times you want it to repeat (3).

If that short way doesn't work use this:
Code: Lua
  1. battery = 100
  2.  
  3. function Battery()
  4.     battery = battery - 1
  5. end
  6.  
  7. timer.Create( "BatteryDecreaser", 3, 1, Battery )

Oh, and if the second number value is turned to 0, you'll want to use timer.Destroy (or timer.Remove).
« Last Edit: June 14, 2015, 07:49:25 pm by Tomzen »
Finished:
Impersonate
<==> FakePromote/Demote <==> RandomMap <==> ForceMic <==> Search <==> PlayMenu <==
WIP:
ServerMode <==

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Best way to decrease a value every x sec
« Reply #3 on: June 14, 2015, 07:47:33 pm »
To do this, you can't use a local variable. You wouldn't be able to call it outside of its current state.

Offline Tomzen

  • Full Member
  • ***
  • Posts: 115
  • Karma: -1
  • A new lua adventurer
    • Thirdage Gaming
Re: Best way to decrease a value every x sec
« Reply #4 on: June 14, 2015, 07:49:53 pm »
To do this, you can't use a local variable. You wouldn't be able to call it outside of its current state.
Thanks for that reminder, it's updated above, and probably a BIG help to him.
Finished:
Impersonate
<==> FakePromote/Demote <==> RandomMap <==> ForceMic <==> Search <==> PlayMenu <==
WIP:
ServerMode <==

  • Print