I was wondering if I should destroy the metatable after using it and also I am not an experienced Programmer and also would like to get tips
local Round = {}
Round.IntermissionTime = 15
function Round.new(intermissionTime)
local self = setmetatable(Round, {__index = Round})
self.IntermissionTime = intermissionTime
return self
end
function Round:CountDown(Duration:number)
local Coroutine = coroutine.create(function()
while Duration >= 0 do
print(Duration)
Duration -= 1
task.wait(1)
end
end)
coroutine.resume(Coroutine)
end
function Round:TimerShift(Value:number)
--This Will increment or Decrease Timer
end
return Round
Since you are not an experienced programmer, I would not recommend using OOP, it will just make it harder for you to understand and overcomplicate things for no reason.
But it depends on how you structure the code, if you are going to add the game functions inside the class, maybe can be useful to destroy it, otherwise if it is just a timer, then just use a function.
local roundTimer = {}
roundTimer.__index = roundTimer
function roundTimer.new()
local self = setmetatable({}, roundTimer)
self.Intermission = 15 -- 15 seconds by default
self.Current = 0
self.Thread = nil
return self
end
-- increases the timer
function roundTimer:Increase()
end
-- decreases the timer
function roundTimer:Decrease()
end
-- starts the timer
function roundTimer:Countdown(duration: number)
if self.Thread and coroutine.status(self.Thread) == "suspended" then -- if there is a timer running then we will cancel it
task.cancel(self.Thread)
self.Thread = nil
end
-- set the current timer to the duration
self.Current = duration
self.Thread = task.spawn(function() -- creates a new thread where the timer will run
while self.Current > 0 do
print(self.Current)
task.wait(1)
self.Current -= 1
end
end)
end
return roundTimer
You can create a new method to destroy the object if you don’t need it anymore.
In fact, I did think about using it like that (Making a Current Timer), but I just feel like it’s always wrong. I always want to find a way to improve my code