How can I improve this Clock System?

How can I improve this Clock System?

So I’ve done some math to make this clock system but the way it works kinda makes me feel a little disorganized. I also feel like this could cause unneeded lag?

Module script code:

module.NightToDay = function(timeSet)
	local starTime = 0 -- 12:00 AM in 24 hour format
	local endTime = 8 * 60 -- 8:00 AM in minutes
	local duration = 3 -- total duration in seconds
	local stepsPerSecond = 20 -- number of steps per second
	local totalSteps = duration * stepsPerSecond
	local interval = (endTime - starTime) / totalSteps 
	local info = TweenInfo.new(timeSet)
	ts:Create(lighting,info,{ClockTime = 8,Brightness = 3}):Play()
	local last = ts:Create(atmo,info,{Density = .3})
	last:Play()
	task.spawn(function() -- does the clock time
		for currentTime = starTime,endTime,interval do
			local hours = math.floor(currentTime / 60) % 12
			hours = (hours == 0) and 12 or hours
			local minutes = currentTime % 60
			
			innerClock.Text = string.format("%02d:%02d",hours,minutes) -- changes the clock time
			wait(1 / stepsPerSecond) -- waits the calculated time
		end
	end)
	last.Completed:Wait()
		return (last.PlaybackState == Enum.PlaybackState.Completed)
end

Is there any way I could improve this function?

1 Like

I guess make it a object

ex.)

local clk = Clock.new()
clk:Start()

Source Code Example:

local Clock = { }
local _Clock = { }

local TS = game:GetService("TweenService") -- Tween Service
local LS = game:GetService("Lighting") -- Lighting Service

function Clock.new()
    local self = { }
    self.startTime = 0
    self.endTime = 8 * 60
    self.duration = 3
    self.stepsPerSecond = 20
    self.totalSteps = self.duration * self.stepsPerSecond
    self.interval = ( self.endTime - self.startTime )

    self.info = TweenInfo.new(  )
    return setmetatable(self, {
        __index = _Clock
    })
end

function _Clock:Start()
    TS:Create(LS, self.info, { ClockTime = 8; Brightness = 3; }):Play()
    local last = TS:Create(atmo, self.info, { Density = .3; })
    last:Play()
    task.spawn(function()
        for currentTime = self.startTime, self.endTime, self.interval duration
            local hrs = math.floor(self.currentTime / 60) % 12
            hrs = ( hrs == 0 ) and 12 or hrs
            
            local mins = currentTime % 60
            
            innerClock.Text = string.format("%02d:%02d", hrs, mins)
            wait(1 / stepsPerSecond)
        end
    end)
    
    last.Completed:Wait(); return (last.PlaybackState == Enum.PlaybackState.Completed)
end

return Clock
1 Like

Put the variables that don’t change (constants) on top of the module, not inside the function. Use x // number instead of math.floor(x / number) You should also be using task.wait instead of wait.

Code:

local starTime = 0 -- 12:00 AM in 24 hour format
local endTime = 8 * 60 -- 8:00 AM in minutes
local duration = 3 -- total duration in seconds
local stepsPerSecond = 20 -- number of steps per second
local totalSteps = duration * stepsPerSecond
local interval = (endTime - starTime) / totalSteps 

module.NightToDay = function(timeSet)	
	local info = TweenInfo.new(timeSet)
	
	ts:Create(lighting, info, {ClockTime = 8, Brightness = 3}):Play()
	
	local last = ts:Create(atmo, info, {Density = 0.3})
	last:Play()
	
	task.spawn(function() -- does the clock time
		for currentTime = starTime, endTime, interval do
			local hours = currentTime // 60 % 12
			hours = (hours == 0 and 12) or hours
			
			-- changes the clock time
			local minutes = currentTime % 60
			innerClock.Text = string.format("%02d:%02d", hours, minutes)
			
			-- waits the calculated time
			task.wait(1 / stepsPerSecond)
		end
	end)
	
	last.Completed:Wait()
	
	return last.PlaybackState == Enum.PlaybackState.Completed
end

what exactly does the “x // answer” do and why is it better than math.floor()?

It’s more efficient that math.floor, and it does the exact same thing.

Educative Answers - Trusted Answers to Developer Questions (the Python example in this article is what Roblox has)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.