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
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