Making a RoundSystem OOP

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.

2 Likes

Well, On the Timer I want to Manipulate to Force the Game End or Increase or Decrease

You should add the __index metamethod too btw.

Here’s how I would do it:

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.

1 Like

It does have the __index. I am not suppose to manipulate the original data?

That a Really smart Way to do it

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

1 Like

Oh, mb, I didn’t see it, sorry. I was in a rush lol

1 Like

No Problem, Theres a lot of ways setting __index I belive