I have a script that activates a aurora borealis at night by using clocktime, however when i test the game it just never works despite there being no errors, does somebody know how to fix this? btw this is in a local script
while true do
if game.Lighting.ClockTime >= 19 and game.Lighting.ClockTime <= 6.1 and game.Workspace.AuroraModel.AuroraBorealis.Beam1.Enabled == false then
game.Workspace.AuroraModel.AuroraBorealis.Beam1.Enabled = true
game.Workspace.AuroraModel.AuroraBorealis.Beam2.Enabled = true
elseif game.Lighting.ClockTime < 19 and game.Lighting.ClockTime > 6.1 and game.Workspace.AuroraModel.AuroraBorealis.Beam1.Enabled == true then
game.Workspace.AuroraModel.AuroraBorealis.Beam1.Enabled = false
game.Workspace.AuroraModel.AuroraBorealis.Beam2.Enabled = false
end
wait()
end
Separate each task (enabling/disabling the beams, determining if it’s time to show the auroras, updating on a loop) into individual functions.
Instead of updating on a loop, update when ClockTime changes.
See shouldShowAurora below for how the condition should be formatted.
local auroraBorealis = workspace.AuroraModel.AuroraBorealis
local beam1, beam2 = auroraBorealis.Beam1, auroraBorealis.Beam2
local function setAuroraBorealisEnabled(enabled)
beam1.Enabled = enabled
beam2.Enabled = enabled
end
-- Protip: you could move the 19 and 6.1 to attributes called
-- "Sunset" and "Sunrise" to make adjusting easier :-)
local function shouldShowAurora()
return game.Lighting.ClockTime >= 19 or game.Lighting.ClockTime <= 6.1
end
local function update()
setAuroraBorealisEnabled(shouldShowAurora())
end
-- Listen for changes and update accordingly
update()
game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(update)
Well, the sample I provided I’m passing the result of shouldShowAurora(), which returns a boolean from the condition inside of it, to the function setAuroraBorealisEnabled. No if-statement is necessariy here, because I’m directly setting Enabled on the Beam objects in that function. I mean, you could do something like this:
if shouldShowAurora() then
setAuroraBorealisEnabled(true)
else
setAuroraBorealisEnabled(false)
end
…but that would be redundant! We could also just put the condition in that if-statement, but again, I’ve separated out the tasks (eg “determine if it’s aurora time”) into functions:
if game.Lighting.ClockTime >= 19 or game.Lighting.ClockTime <= 6.1 then
setAuroraBorealisEnabled(true)
else
setAuroraBorealisEnabled(false)
end
-- also the same as:
setAuroraBorealisEnabled(game.Lighting.ClockTime >= 19 or game.Lighting.ClockTime <= 6.1)
it still is doing the same thing where it does not work but dosen’t error, this i put this together incorrectly?
local auroraBorealis = workspace.AuroraModel.AuroraBorealis
local beam1, beam2 = auroraBorealis.Beam1, auroraBorealis.Beam2
local function setAuroraBorealisEnabled(enabled)
beam1.Enabled = enabled
beam2.Enabled = enabled
end
-- Protip: you could move the 19 and 6.1 to attributes called
-- "Sunset" and "Sunrise" to make adjusting easier :-)
local function shouldShowAurora()
if game.Lighting.ClockTime >= 19 or game.Lighting.ClockTime <= 6.1 then
setAuroraBorealisEnabled(true)
else
setAuroraBorealisEnabled(false)
end
end
local function update()
setAuroraBorealisEnabled(shouldShowAurora())
end
-- Listen for changes and update accordingly
update()
game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(update)
The job of shouldShowAurora is to return a boolean value (true/false) indicating if it’s aurora time. To return that boolean, all you need is the condition. From my original post, this is all you need.
local function shouldShowAurora()
return game.Lighting.ClockTime >= 19 or game.Lighting.ClockTime <= 6.1
end
while true do
if game.Lighting.ClockTime >= 19 and game.Lighting.ClockTime <= 6.1 and game.Workspace.AuroraModel.AuroraBorealis.Beam1.Enabled == false then
game.Workspace.AuroraModel.AuroraBorealis.Beam1.Enabled = true
game.Workspace.AuroraModel.AuroraBorealis.Beam2.Enabled = true
elseif game.Lighting.ClockTime < 19 and game.Lighting.ClockTime > 6.1 and game.Workspace.AuroraModel.AuroraBorealis.Beam1.Enabled == true then
game.Workspace.AuroraModel.AuroraBorealis.Beam1.Enabled = false
game.Workspace.AuroraModel.AuroraBorealis.Beam2.Enabled = false
end
wait()
break
end
In update - look closely at what it does. I provided this as a secondary example to explain how the code I provided might look differently, depending on your preference or level of familiarity.
This doesn’t really make any sense, because that would make the loop redundant. If you were using a loop, you’d want it to keep going and going and going…
Yes, but otherwise it won’t run any other code below. I’d assume there’s more code below. If the infinite loop is necessary, then don’t add the break. I had posted before: