1

I have this set in my lua reload section and I'd love to set a cooldown function for it like how there is for primaryfire and secondary fire. Is there anyway to do that? Here's my code.

function SWEP:Reload()
    if Chaos == 0 then
        Chaos = 1
        self.Owner:SetModel("models/_tails_ models/characters/sonic heroes/super_sonic/supersonic.mdl")
        self.Weapon:EmitSound( "weapons/now.wav" )
    elseif Chaos == 1 then
        Chaos = 0
        self.Owner:SetModel("models/_tails_ models/characters/sonic heroes/sonic/sonic.mdl")
    end
end
Sacred Myths
  • 13
  • 1
  • 5

1 Answers1

3

os.time() should do the trick. You can take a look at the documentation at the Lua website.

The logic behind allowing something to happen only after sometime is to check the time elapsed since the last time the function was used. Logically, it would be -

timeElapsed = lastTimeOfUse - timeNow

If timeElapsed > cooldownPeriod then allow the event to take place and set the lastTimeOfUse = timeNow.


If you mean something like reload function will work only after 60 (change it to anything) seconds, try the following :-

-- Settings
cooldown = 60 -- Cooldown period in Seconds

-- Reload function with cooldown
local lastReloadTime=0;
function SWEP:Reload()
    if ((os.time()-lastReloadTime)>cooldown) then -- Allows only after cooldown time is over
        if Chaos == 0 then
            Chaos = 1
            self.Owner:SetModel("models/_tails_ models/characters/sonic heroes/super_sonic/supersonic.mdl")
            self.Weapon:EmitSound( "weapons/now.wav" )
        elseif Chaos == 1 then
            Chaos = 0
            self.Owner:SetModel("models/_tails_ models/characters/sonic heroes/sonic/sonic.mdl")
        end
        lastReloadTime=os.time() -- Sets this time as last using time of Reload
    end
end

Based on your comment, if you want to loop the sound till a specific time, something like this should work

 -- Settings
durationOfPlayback = 3 -- for how long you want to play the sound in seconds

-- Specifications
durationOfSoundFile = 1 -- length of sound file in seconds

-- Sound playback for a specific time cooldown
noOfTimesToPlay = math.floor(durationOfPlayback/durationOfSoundFile)
function SWEP:Reload()
    ...
    for i = 1, noOfTimesToPlay do
    {
            self.Weapon:EmitSound( "weapons/now.wav" )
            lastSoundTime=os.time()

            --This line will make the loop wait till 1 playback is complete
            while((os.time()-lastSoundTime)<durationOfSoundFile) do end
    }
    ...
end
Chique
  • 735
  • 3
  • 15
  • I'm sorry for any confusion I was in abit of rush. But, I edited my question for what I meant. I'll post it here too. "What I mean is like is there a way I can set a timer for the emitsound part, sorry for any confusion." Actually what you provided may be just what I need, but might as well ask since I'm curious. Is there anyway to set a timer on the self.weapon:emitsound part. – Sacred Myths Sep 02 '14 at 21:06
  • Disregard all I've said that worked! Thanks alot man! I really appreciate it :) – Sacred Myths Sep 02 '14 at 21:19
  • Yep, then you would have to use the same technique for the emit sound part too, using another variable called lastSoundTime maybe, and looping it till the cool-down period is over. Let me try to edit my reply. You are most welcome :) Keep up the great work and put down your imagination. – Chique Sep 02 '14 at 22:33
  • Idk where my other comment went so I'll just sum up what I said. Thanks alot dude! I really appreciate you taking the time to help me. – Sacred Myths Sep 03 '14 at 00:58