[Lua] Adding a cooldown to your gossip script menu

User Tag List

Results 1 to 5 of 5
  1. #1
    Dynashock's Avatar Contributor

    Reputation
    176
    Join Date
    Nov 2007
    Posts
    203
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Lua] Adding a cooldown to your gossip script

    Disclaimer: For fully understanding of this guide you must understand what tables are and how they work, and how a gossip script works.

    Ever wanted to prevent someone from clicking a dozen times on an item, object, or NPC gossip script? Ever wanted to add a cooldown? More importantly, ever wanted to add a player specific cooldown and not a general cooldown for the script? Well, it's possible and I'll be teaching you how to in this guide.

    New function(s):
    - os.clock(), retrieves the time in seconds since the Lua Engine has started.

    For this guide I'll be using this item gossip. I will not be explaining how everything of a gossip works because that's not what this guide is meant for.
    Code:
    CooldownTime = {}
    
    function S_Script(item, event, player)
    	Script(item, player)
    end
    
    function Script(item, player)
    	if CooldownTime[player:GetName()] ~= nil and ((os.clock()-CooldownTime[player:GetName()])) <= 5 then
    		player:SendAreaTriggerMessage("|cFFFF0000You cannot use this item yet!")
    	else
    		CooldownTime[player:GetName()] = os.clock()
                    REST OF SCRIPT
    end
    
    RegisterItemGossipEvent(57045, 1, "S_Script")
    I'll be explaining it with code snippets.

    Code:
    CooldownTime = {}
    Define CooldownTime as a table. If we would not the Lua engine would think it was a variable. By making them empty we make sure that everyone/everything starts clean off when the script is loaded.

    Code:
    	else
    		CooldownTime[player:GetName()] = os.clock()
                    REST OF SCRIPT
    The reason why I explain this part before the if part is because you need to know what happens here before you can understand the if statement. Once the gossip's run, it will add the current Lua engine time to the table CooldownTime with as index the player's name. Say the engine has been running for 30 seconds the table would look like "Dynashock 30".

    Code:
    	if CooldownTime[player:GetName()] ~= nil and ((os.clock()-CooldownTime[player:GetName()])) <= 5 then
    		player:SendAreaTriggerMessage("|cFFFF0000You cannot use this item yet!")
           else
    First we check if there's a value for the index player:GetName(). This check is needed to prevent an error from happening as otherwise it would try to substract NIL ('emptyness') from a number, which is not possible of course. If it indeed is empty it will break off the if statement there already and do everything beneath else. If the value for the index player:GetName() isn't empty it will do the os.clock() time minus the value contained in CooldownTime[player:GetName()] (= the time of when the player clicked). If this this time is smaller or equal to 5 seconds it will send the message. Thus this means 5 seconds is our cooldown time, if we want to either higher or lower the cooldown we will have to adjust this number.

    Why not just do it with RegisterEvent and reset a value after x seconds?
    Objects and items cannot register events and this looks cleaner to me.

    I hope this guide has been any helpful for you. Feel free to ask any questions should you have them.

    [Lua] Adding a cooldown to your gossip script
  2. #2
    somaz's Avatar Member
    Reputation
    1
    Join Date
    Jan 2009
    Posts
    5
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ThnnX man + rep

  3. #3
    Claiver's Avatar Member
    Reputation
    138
    Join Date
    Mar 2009
    Posts
    217
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I cannot understand how this post of yours has only gotten 1 comment..
    What's wrong with people!?

  4. #4
    Meiya Stormsinger's Avatar Contributor

    Reputation
    163
    Join Date
    Mar 2009
    Posts
    196
    Thanks G/R
    0/2
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *bump* This is awesome, and it should be used more imo. It has helped me plenty +rep to you dyna!

  5. #5
    MaCAppLeBiTeMe's Avatar Private
    Reputation
    1
    Join Date
    Jul 2010
    Posts
    6
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is great. Thank you very much. +rep. Not like mine matters though.

Similar Threads

  1. [Lua Script] Need help with this lua gossip script
    By diviee3 in forum WoW EMU Questions & Requests
    Replies: 1
    Last Post: 06-21-2010, 07:52 PM
  2. [Lua] Gossip Script
    By LJN in forum World of Warcraft Emulator Servers
    Replies: 9
    Last Post: 07-20-2008, 04:38 AM
  3. [share] mini-tut on gossip scripts lua
    By b!atch in forum World of Warcraft Emulator Servers
    Replies: 2
    Last Post: 05-29-2008, 05:11 AM
  4. Adding aditional Features to your website!
    By Laurenzb in forum WoW EMU Guides & Tutorials
    Replies: 4
    Last Post: 12-20-2007, 06:08 AM
  5. Gossip scripting guide!
    By Le Froid in forum WoW EMU Guides & Tutorials
    Replies: 3
    Last Post: 12-13-2007, 05:06 PM
All times are GMT -5. The time now is 06:29 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search