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
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)
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.
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