• Print

Author Topic: ulx cookie  (Read 5356 times)

0 Members and 1 Guest are viewing this topic.

Offline GreenNoizuf

  • Newbie
  • *
  • Posts: 5
  • Karma: 0
ulx cookie
« on: March 16, 2015, 04:15:45 am »
I would like to start by saying that I am new to Lua and am trying to teach myself, so plz no hate <3

Hey, I made a pointless cookie command for my deathrun server called ulx cookie.  All it does is show "User1 gave User 2 a cookie!" and uncookie: "user1 took a cookie from user2"
This command is a lot of fun and tbh attracts a lot of people to the server, however it is overused a fair bit.  I was wanting to make a timer that restricts how much cookies a person can give, e.g. someone can only give a cookie to someone else every 5 seconds.  I successfully did this but there is one small problem: The timer is global and not separate for each person (if that makes sense) e.g. if I give a cookie to a player, it stops everyone in the server from giving a cookie for 10 seconds.  Now what i would like is for me to be able to give someone a cookie, and everyone else be unrestricted by my cookie giving.  In other words, I only want the cookie giver to be affected by the timer.
Could someone please give me some advice or help me with this troublesome command? it would be much appreciated :p

Here is the curent code:
Code: Lua
  1. ------------------------------ Cookie ------------------------------
  2. local waittime = 10
  3. local lasttimeuseage = -waittime
  4. function ulx.cookie( calling_ply, target_plys )
  5.         if lasttimeusage + waittime > CurTime() then
  6.                 ULib.tsayError( calling_ply, "Please wait " .. waittime .. " seconds before using this command again", true )
  7.                 return
  8.         end
  9.        
  10.         lasttimeusage = CurTime()
  11.         ulx.fancyLogAdmin( calling_ply, "#A gave #T a cookie!", target_plys )
  12. end
  13. local cookie = ulx.command( CATEGORY_NAME, "ulx cookie", ulx.cookie, "!cookie" )
  14. cookie:addParam{ type=ULib.cmds.PlayersArg }
  15. cookie:defaultAccess( ULib.ACCESS_ADMIN )
  16. cookie:help( "Spread the love with cookies!." )

Offline lynx

  • Jr. Member
  • **
  • Posts: 59
  • Karma: 15
Re: ulx cookie
« Reply #1 on: March 16, 2015, 05:45:19 am »
The best way to do a wait time (in my opinion) is this:

Code: Lua
  1. --Function stuff--
  2. if !ply.nextcookie or ply.nextcookie > CurTime() then
  3.     --do cookie stuff--
  4.     ply.nextcookie = CurTime()+waittime
  5. else
  6.     --do not cookie stuff--
  7. end
  8. --end function stuff
  9.  

Of course that requires modification to how you do the code stuff (such as calling_ply instead of ply, etc)

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: ulx cookie
« Reply #2 on: March 16, 2015, 03:04:15 pm »
Check out ULX's anti-spam code too (chat.lua I think)
It has the basic idea lynx indicates.
Though, your original code, as is, could be fun... could tell the person trying to use the command, "sorry, cookie jar is empty right now - <Last player to use the command> gave away the last cookie!"
As a fun learning project, make it randomly do that. :P


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

Offline GreenNoizuf

  • Newbie
  • *
  • Posts: 5
  • Karma: 0
Re: ulx cookie
« Reply #3 on: March 16, 2015, 06:05:56 pm »
Check out ULX's anti-spam code too (chat.lua I think)
It has the basic idea lynx indicates.
Though, your original code, as is, could be fun... could tell the person trying to use the command, "sorry, cookie jar is empty right now - <Last player to use the command> gave away the last cookie!"
As a fun learning project, make it randomly do that. :P
aha that might actually be a fun idea :p.
Thankyou both for the help!, ill check out the chat.lua and see if i learn something :p
thanks lynx for the code, ill see if i can get it to work :p

Offline GreenNoizuf

  • Newbie
  • *
  • Posts: 5
  • Karma: 0
Re: ulx cookie
« Reply #4 on: March 16, 2015, 09:11:33 pm »
The best way to do a wait time (in my opinion) is this:

Code: Lua
  1. --Function stuff--
  2. if !ply.nextcookie or ply.nextcookie > CurTime() then
  3.     --do cookie stuff--
  4.     ply.nextcookie = CurTime()+waittime
  5. else
  6.     --do not cookie stuff--
  7. end
  8. --end function stuff
  9.  

Of course that requires modification to how you do the code stuff (such as calling_ply instead of ply, etc)
hmm if put this into the code, and it doesn't work, I want to just add a personal timer for now, does this need anything added to the end function stuff or start function stuff?

the code is:
Code: Lua
  1. local CATEGORY_NAME = "Grinny/Green's stuff"
  2.  
  3. local waittime = 5
  4. function ulx.cookie(calling_ply, target_plys)
  5.         if !ply.nextcookie or ply.nextcookie > CurTime() then
  6.                 ulx.fancyLogAdmin(calling_ply, "#A gave #T a cookie!", target_plys)
  7.                 ply.nextcookie = CurTime()+waittime
  8.         else
  9.                 ULib.tsayError(calling_ply, "Please wait " .. waittime .. " seconds before using this command again", true)
  10.         end
  11. end
  12. local cookie = ulx.command( CATEGORY_NAME, "ulx cookie", ulx.cookie, "!cookie" )
  13. cookie:addParam{ type=ULib.cmds.PlayersArg }
  14. cookie:defaultAccess( ULib.ACCESS_ADMIN )
  15. cookie.help( "spread the love with cookies!" )
  16.  
  17.  
  18.  
  19.  
« Last Edit: March 16, 2015, 09:31:19 pm by GreenNoizuf »

Offline Avoid

  • Full Member
  • ***
  • Posts: 142
  • Karma: 42
Re: ulx cookie
« Reply #5 on: March 17, 2015, 01:35:42 am »
Hello,
you do not have a valid ply object here, use the calling_ply/target_ply instead. Also the timer does look correct to me.

Cheers,
Avoid

Offline GreenNoizuf

  • Newbie
  • *
  • Posts: 5
  • Karma: 0
Re: ulx cookie
« Reply #6 on: March 17, 2015, 04:50:46 am »
decided to use the global timer idea with the "the cookie jar is empty" line.
thanks all please post improvements you might have or if you know how to add a timer for individuals that would also be great for learning purposes.

here is the finished code:
Code: Lua
  1. CATEGORY_NAME = "Grinny's Stuff" --Change this for the category name of choice, this will be the tab that the command is under in the !menu
  2. -----------------------------Cookie-----------------------------
  3. local waittime = 5 --Change this value for a differ in wait time after !cookie has been executed
  4. local lastusecookie = -waittime
  5. function ulx.cookie(calling_ply, target_plys, times)
  6.         if waittime + lastusecookie > CurTime() then
  7.                 ULib.tsayError(calling_ply, "Sorry, the cookie jar is empty! Please wait " .. waittime .. " seconds for it to fill up")
  8.         else   
  9.                 ulx.fancyLogAdmin( calling_ply, "#A gave #T #i cookies!", target_plys, times )
  10.                 lastusecookie = CurTime()
  11.         end
  12. end
  13. local cookie = ulx.command( CATEGORY_NAME, "ulx cookie", ulx.cookie, "!cookie" )
  14. cookie:addParam{ type=ULib.cmds.PlayersArg }
  15. cookie:addParam{ type=ULib.cmds.NumArg, min=1, default=1, hint="times", ULib.cmds.optional, ULib.cmds.round }
  16. cookie:defaultAccess( ULib.ACCESS_ADMIN )
  17. cookie.help( "spread the love with cookies!" )

as you can see i added a cookie counter so to speak, where users can select how many cookies to give people.
this code all works well (although for some reason any functions below it do not work? i dont know why this happens but if i put any new commands that i make below it in the same file they just dont work, however if i but the cookie command below the other command it does? if anyone knows how to fix that it would be great, its not a huge problem, i just have a seperate file for uncookie as it does the same thing so i cant have them both in the same file. )
Thanks for all your help!

Offline Aaron113

  • Hero Member
  • *****
  • Posts: 803
  • Karma: 102
Re: ulx cookie
« Reply #7 on: March 17, 2015, 09:59:39 am »
I believe your code might be erroring and that is why you cannot put more code underneath it.

Just replace the period with a colon to fix.
Code: [Select]
cookie:help( "spread the love with cookies!" )

  • Print