I've got a script that changes time, which works perfectly, and also a script which detects time of day, which works, but is inefficient.
lights = script.Parent:GetChildren() game.Lighting.LightingChanged:connect(function() local tam = game.Lighting:GetMinutesAfterMidnight() if tam >= 390 and tam <= 1020 then for i,v in pairs(lights) do if v.Name == "Light" then v.PointLight.Enabled = false v.Material = Enum.Material.Plastic end end else for i,v in pairs(lights) do if v.Name == "Light" then v.PointLight.Enabled = true v.Material = Enum.Material.Neon end end end end)
The reason for inefficiency is that it would enable the lights every time the time changes, which is obviously inefficient and laggy. But I don't know how to make it more efficient.
The time script is
local start, starthour = tick(), game.Lighting:GetMinutesAfterMidnight() local speedModifier = 0.5 local gameTime = 0 lastTick = tick() while true do local delta = tick()-lastTick gameTime = gameTime + delta * speedModifier game.Lighting:SetMinutesAfterMidnight( gameTime + starthour) lastTick = tick() game:service('RunService').Stepped:wait() end
To remove duplicate request when they are not needed just include a debounce so the code block only runs when it is needed.
I have also moved some code into a function to help you.
local lighting = game.Lighting local runService = game:GetService('RunService') local lights = script.Parent:GetChildren() local isNeon = false -- acts as a debounce lighting.LightingChanged:connect(function() local tam = lighting:GetMinutesAfterMidnight() if tam >= 390 and tam <= 1020 then setLights(Enum.Material.Plastic, false) elseif not isNeon then -- alos check for debounce setLights(Enum.Material.Neon, true) end end) -- use a function you had buplicate coded function setLights(material, pointLight) isNeon = pointLight -- this acts as a debounce for i,v in pairs(lights) do if v.Name == "Light" then v.PointLight.Enabled = pointLight v.Material = material end end end local start, starthour = tick(), lighting:GetMinutesAfterMidnight() local speedModifier = 0.5 local gameTime = 0 local lastTick = tick() -- delta removed we dont need to store it while true do gameTime = gameTime + (tick()-lastTick) * speedModifier lighting:SetMinutesAfterMidnight( gameTime + starthour) lastTick = tick() runService.Stepped:wait() end
Hope this answers your question, comment if you need more info.