Hi. I have recently learnt the fundamentals of OOP within Roblox. Using this new found knowledge, I decided to rescript the cooldown system of my game to become object-oriented. The purpose of this post is to ensure that I am doing OOP correctly. So here is some questions to achieve that goal:
Does the module follow the standard OOP practices/procedures?
Is there anyway I can improve and make the code more efficient?
Module:
--[[
Cooldown Handler
Bindable remote based.
Allows the creation of cooldowns which can be restarted while they are still "counting down".
]]
local cooldowns = {}
cooldowns.__index = cooldowns
function cooldowns.new(selfScript, name, duration)
local cooldownObject = {}
setmetatable(cooldownObject, cooldowns)
cooldownObject.Name = name
cooldownObject.Duration = duration
cooldownObject.Event = Instance.new("BindableEvent")
cooldownObject.Event.Name = name
if selfScript:FindFirstChild("RuntimeCooldowns") then
cooldownObject.Event.Parent = selfScript.RuntimeCooldowns
else
local folder = Instance.new("Folder")
folder.Parent = selfScript
folder.Name = "RuntimeCooldowns"
cooldownObject.Event.Parent = folder
end
cooldownObject.Active = false
return cooldownObject
end
function cooldowns:start()
--[?] Wrap in coroutine because module functions yield the current thread
coroutine.wrap(function()
local threadDisabled = false
self.Event:Fire()
self.Active = true
self.Event.Event:Connect(function()
threadDisabled = true
end)
task.wait(self.Duration)
if not threadDisabled then
self.Active = false
end
end)()
end
function cooldowns:stop()
coroutine.wrap(function()
self.Event:Fire()
self.Active = false
end)()
end
return cooldowns
I apologise if this is under the wrong tag, I don’t know whether to put this under scripting support or creations feedback.