I am practicing different things in Lua, having learned it in a day and is understandable since I program in different languages. Although, I am unsure why or even how I’ve gotten an error in my code (Issue).
Goal
Whenever it is 14:00:00 or later, or whenever it is 20:00:00 or earlier, a point-light should be “Enabled”.
Issue
The last operator a part of the condition is underlined in ROBLOX Studios.
Concern
Why does the operator not work in this case?
I have tried reading on multiple DevForum posts, as well as reading the Luau language documentation with no luck in finding out a solution to the code error.
while wait() do
if game.Lighting.ClockTime >= 14 or <= 20 then
workspace.testPart.PointLight.Enabled = true
print("Night")
else
workspace.testPart.PointLight.Enabled = false
print("Day")
end
end
Let me know if I have missed something about the code and feel free to link any external links in assistance to solving the issue.
So here’s the thing, LuaU thinks you want to compare “or” so to fix it you need to do game.Lighting.ClockTime after “or” and after you do that add the operators and stuff you already have addded.
You need to write game.Lighting.ClockTime >= 14 or game.Lighting.ClockTime <= 20 . This is because Lua doesn’t take into account the previous condition when you use “and” or “or”
if game.Lighting.ClockTime >= 14 or game.Lighting.ClockTime <= 20 then
code:
while wait() do
if game.Lighting.ClockTime >= 14 or game.Lighting.ClockTime <= 20 then
workspace.testPart.PointLight.Enabled = true
print("Night")
else
workspace.testPart.PointLight.Enabled = false
print("Day")
end
end
These replies have partly solved my issue. My confirmation (prints) are now looping correct, but the point-light has not enabled during the night.
-- STORED DATA
local newPart = Instance.new("Part") -- variable for part
newPart.Name = "Light" -- name for its use
newPart.Size = Vector3.new(3,3,3) -- 3-dimensional size
newPart.Anchored = true -- locked in space
newPart.Position = Vector3.new(-24.91, 25.984, 21.28) -- 3-dimensional position in space
newPart.Parent = workspace -- child of...
local newLight = Instance.new("PointLight") -- creation of the light source itself
newLight.Parent = newPart -- child of the physical light object
------------------------------------------------------------------
-- PRACTICAL CODE
while wait() do -- loop forever with a 0,03s delay (dedicated light script)
if game.Lighting.ClockTime >= 18 or game.Lighting.ClockTime <= 4 then -- if hour of day is equal to 18 or 4, or greater than 18 (6 PM) but less than 4 (4 AM)
newLight.Enabled = true -- light source is enabled
print("Night") -- confirm enabled source via print
else
newLight.Enabled = false -- light source is disabled
print("Day") -- confirm disabled source via print
end
end
I was too lazy to add the actual objects through the explorer.
This might look a bit messy with the comments, but in Studios, my custom theme has a very dark color (yet readable) for comments. That means I could have a paragraph for a single print line of code and it would still look clean.
Maybe try adjusting the range and brightness. I’m not sure what Roblox sets those 2 values to when you create a PointLight with Instance.new(), so yeah, try doing something like: