Roblox OOP cooldown feedback

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.

depends, oop is usually used to model real world objects, it all really depends on your use case,
may i ask what your use case is ?

forgot to add, metatables are used to mimic oop but it will never be true oop sadly

before, i had to use delay() for cooldown. This would result in very repetitive code and make it even more difficult for cooldowns to reset while they were counting down.

is this for like some sort of power or ability feature?
like E to bring lightning kinda thing?

A lot more advanced than that. A combat system with multiple skills with “sub-cooldowns”.
For example:
You do a basic hit.
There is a 0.5 second delay between doing another basic hit (main cooldown). But there is a 1 second cooldown until you can do a heavy hit (sub-cooldown)

You could model these combat moves into some sort of Object too and implement a cooldown system within it

that’s what i’d probably do

In my opinion, your OOP code looks fine.

The only thing is triggering is the fact you don’t knowthis exists as a replacement for delay()!

1 Like