Created
August 27, 2021 02:16
-
-
Save Vurv78/39f8fcc30d3ca0a54a7b552ec4645876 to your computer and use it in GitHub Desktop.
Burst limit in lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Burst Limit for Garrysmod/Wiremod | |
local Burst = setmetatable({}, { | |
__call = function(self, max, regen_time) | |
return setmetatable({ | |
max = max, -- Will start full. | |
regen = regen_time, | |
tracker = WireLib.RegisterPlayerTable() | |
}, self) | |
end | |
}) | |
Burst.__index = Burst | |
function Burst:get(ply) | |
local data = self.tracker[ply] | |
if data then | |
return data.stock | |
else | |
return self.max | |
end | |
end | |
function Burst:check(ply) | |
local data = self.tracker[ply] | |
if data then | |
local last = data.last | |
local stock = data.stock | |
if stock == 0 then | |
stock = math.min( math.floor(last / self.regen), self.max) | |
if stock == 0 then | |
return false | |
end | |
else | |
return true | |
end | |
else | |
return true | |
end | |
end | |
function Burst:use(ply) | |
local data = self.tracker[ply] | |
if data then | |
local last = data.last | |
local stock = math.min( data.stock + math.floor(last/self.regen), self.max ) | |
if stock > 0 then | |
data.stock = stock - 1 | |
data.last = CurTime() | |
return true | |
else | |
return false | |
end | |
else | |
self.tracker[ply] = { | |
stock = self.max - 1, | |
last = CurTime() | |
} | |
return true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment