Which is Better to do

I am making a Day Night System and I want the lest laggiest one which one do I do

local runservice = game:GetService("RunService")
local lightning = game:GetService("Lighting")

local function updatetime()
	local currenttime = lightning.ClockTime
	local timeinterval = 0.001
	local newtime = currenttime + timeinterval
	lightning.ClockTime = newtime
end

runservice.Heartbeat:Connect(updatetime)

or

local Time = 0.01
local TimeChange = 0.01

while task.wait(Time) do
game.Lighting.ClockTime = game.Lighting.ClockTime + TimeChange
end
4 Likes

The first one is the least laggy one but you should make some changes to make it more optimized:

  • Remove the currenttime and newtime variables.
  • Put the timeinterval variable in the parentheses of the updatetime() function
  • Use x += a instead of x = x + a

Final code should look like:

local runservice = game:GetService("RunService")
local lightning = game:GetService("Lighting")

local function updatetime(timeinterval)
	lightning.ClockTime += timeinterval
end

runservice.Heartbeat:Connect(function()
	updateTime(0.001)
end)
3 Likes

Neither, this should be managed by the Client, however if you want synchronized times for everyone, I recommend you send data to the server to ensure that will happen.

2 Likes

while true and task.wait() is the same as a Heartbeat connection

1 Like

There is a very easy solution to this, from a russian forum member @GAVsi115, uses TweenService:

-- Smooth change of daytime

-- Variables
local DayTimer = 1		-- minutes of real time

----------------------------------------------------------------------------------
local timeChange = DayTimer * 60	-- minutes*seconds -- irl time

local light = game.Lighting
light.ClockTime = 0

local TweenService = game:GetService("TweenService")	-- defining service
local tweenInfo = TweenInfo.new(-- setting variables
	timeChange,                 -- Time
	Enum.EasingStyle.Linear,	-- EasingStyle (style of interpolation)
	Enum.EasingDirection.In,    -- EasingDirection (direction of interpolation)
	-1             				-- RepeatCount (negative value makes it run forever)
)

local result = {ClockTime = 24}	-- set the result massive
local tween = TweenService:Create(light, tweenInfo, result) -- create the tween
tween:Play()                        -- run it
2 Likes

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