#Day and night lamp
129 messages · Page 1 of 1 (latest)
dm me
i have code but it does not work
i am balding trying to develop a solution for this
if game.Lighting.TimeOfDay >= 6 and game.Lighting.TimeOfDay <= 14 then
workspace.LAMP.BrickColor = BrickColor.new("Black")
else
workspace.LAMP.BrickColor = BrickColor.new("New Yeller")
end
print(game.Lighting.TimeOfDay) @fluid grove
** You are now Level 1! **
why doesnt taht work
x=1
while x==1 do
if game.Lighting.ClockTime >= 6 and game.Lighting.ClockTime <= 14 then
workspace.LAMP.BrickColor = BrickColor.new("Black")
else
workspace.LAMP.BrickColor = BrickColor.new("New Yeller")
end
wait(2000)
x=2
end
bro nothing works omg
..........
while true do
If game.Lighting.TimeOfDay > 19 then
Script.Parent.Parent.Lamp.SpotLight.Brightness = 3
Else
Script.Parent.Parent.Lamp.SpotLight.Brightness = 0
End
Wait(20)
End
yeah but he does not know that
@cyan swift @rocky locust @fluid grove GUYS! the dude's a newbie, I would suggest to not just send shitty code w/o explanation, it does not really helps him
alright let's go through it logically.
- First: you need to at least have some sort of daynight cycle. to begin with
you may do that by adding to the MinutesAfterMidnight property of lighting! using the
SetMinutesAfterMidnight() function, you can read about it here https://create.roblox.com/docs/reference/engine/classes/Lighting#SetMinutesAfterMidnight
it even has an example into how to format it to a 24 hour clock (useful since set minute after midnight is that, 24 hours in minute format, so 1440 minutes.)
- Second: Now that you have your day night cycle going, what you would do now is simply check if the time of day is night or day to turn off the lamp
the code might look like this ```lua
local Lighting = game:GetService("Lighting")
local Lamp = workspace.Lamp
local MinutesAfterMidnight = 0 --> let's start exactly at midnight
while true do
MinutesAfterMidnight += 10 --> will add 10 every iteration!
-- now that we have computed the mninutes after midnight! Let's
-- pass the result of that simple equation to lighting to set the time!
Lighting:SetMinutesAfterMidnight(MinutesAfterMidnight)
-- Now let's check if it's day or night if the time is between 6AM and 6PM
-- let's keep the light off, ELSE let's keep it on
if Lighting.ClockTime >= 6 and Lighting.ClockTime <= 18 then
Lamp.PointLight.Enabled = false
else
Lamp.PointLight.Enabled = true
end
task.wait()
end
ok so i have a couple of questions, my first one is the SetMinutesAfterMidnight function thing, i read the stuff and im still confused at what it does and how it is useful . my other question is which part of my code was wrong and how incorrect was it.. i want to understand my own code and what was not correct so i can understand it better. lastly this probably sounds like a stupid question but, what is the point of making a variable local? i think i get what a local variable is, a variable in only a single block i think, but why not just use a normal variable? thanks so much and it would be SUPER helpful if u or someone else answers! :))
sure!
ok so i have a couple of questions, my first one is the SetMinutesAfterMidnight function thing, i read the stuff and im still confused at what it does and how it is useful
like I explained to you
it's literally in the the name
remember the day is divided is 24 hours
and easiest way to work it out w/o math is in its minutes, there are 1440 minutes in a day
Set minutes after midnight is a function that takes any number between 0 and 1440, and sets the time of day to be that, so it converts your minutes to whatever hour it should be
my other question is which part of my code was wrong and how incorrect was it..
I did not read it lol, which code?
lastly this probably sounds like a stupid question but, what is the point of making a variable local?
There are no stupid questions, just stupid people, and you're not stupid! You're very smart for doing that question actually! It shows you care.
-
Remember what variables are! They are values saved in memory therefore using a variable will save in memory a reference OR a copy of your value, depending which it is, making your code faster
-
Even if that wasn't a thing... Would you rather do this?
local MinutesAfterMidnight = 0 --> let's start exactly at midnight
while true do
MinutesAfterMidnight += 10 --> will add 10 every iteration!
-- now that we have computed the mninutes after midnight! Let's
-- pass the result of that simple equation to lighting to set the time!
game.lighting.Lighting:SetMinutesAfterMidnight(MinutesAfterMidnight)
-- Now let's check if it's day or night if the time is between 6AM and 6PM
-- let's keep the light off, ELSE let's keep it on
if game.Lighting.ClockTime >= 6 and game.Lighting.ClockTime <= 18 then
game.workspace.Lamp.PointLight.Enabled = false
else
game.workspace.Lamp.PointLight.Enabled = true
end
task.wait()
end
Much longer to write!
- we make the variable local due to scopes! Definitely read on them if you're curious, but you'll see and understand them soon, if you're very very new, just make all your variables local
Note: In Roblox due to good practice all your variables are gonna be local, the only non local you'll see is functions
all of this helped a lot, the last thing though is my code review so heres my very terrible code
x=1
while x==1 do
if game.Lighting.ClockTime >= 6 and game.Lighting.ClockTime <= 14 then
workspace.LAMP.BrickColor = BrickColor.new("Black")
else
workspace.LAMP.BrickColor = BrickColor.new("New Yeller")
end
x=2
end
imagine i did a while true do instead of whatever i did there
and i also had a day and night system in the game
let me get it
x=1
while x==1 do
if game.Lighting.ClockTime >= 6 and game.Lighting.ClockTime <= 14 then
workspace.LAMP.BrickColor = BrickColor.new("Black")
else
workspace.LAMP.BrickColor = BrickColor.new("New Yeller")
end
x=2
end
yes this code actualyl does nothing hahaha
even if we imagine the while loop, it would not do anything
plus yeah, horrid variable naming ** your code has to read like english ideally**
a bit advanced but the code readability helps a lot! when you name things nicely it helps understand what is even happening
I don't do shit like x or rs or abreviations
full names that describe what the variable is holding
wait so question what i was aiming for was like while true do - if the game clock time (which was moving as the day went on) was greater than 6 AND less than 14 it would be the color black, and otherwise it would be yellow (new yeller)
also yeah i will get better at naming my code
so what is wriong in that logic
let's put it this, let's try to forget about your code, since it's illogical
it doesn't do anything since it's not weel thought
start from scratch and try to logically design it again, do an algorithm!
an algorithm is just a list of steps 
so let's go through it again
- increase the time of day
- if there's daylight then turn off a lamp
- if there's no daylight then turn on a lamp
this is just a list of steps you do when you don't really know how you would do something but know how it should work
so from there we investigate, as you see I gave you the minutes after midnight thingy
and then we can reach the code I sent you
and this rough version actually is more simillar to the list of steps I just said than the cleaner one actually!
@tired inlet hey u taught me a lot ngl rn but i will say, calling the code illogical may have been a tad bit on the wrong path
regardless of the code and what it is, there is always something wrong with it that can be explained, i may not be like a coding expert but i am positive about this
secondly i just looked at my own illogical code for longer and compared it to ur more well written and professional code, trying to understand why your code did not have a run time error and mine did when i had a while do, so i added a task.wait() (i dont rly know what it does) to my code and well now it works so im sure there was some logic in my code
regardless ur code was more nuanced and obviously adept and your explanations taught me quite a lot, so thanks a bunch!!
regardless of the code and what it is, there is always something wrong with it that can be explained, i may not be like a coding expert but i am positive about this
Oh I just called it illogical cause it has not a whole lot of logical structure, hence why it would not run
cause illogical code does not really run
Illogical does not mean runtime errors
it can simply fail to do what you expect
or quietly fail
yeah
I'm glad my explanation was useful btw
wait before u go
could u explain to me one last thing if possible
alright
whats task.wait() and how does it help with running a while loop
** You are now Level 2! **
dont know if thats the right way to ask it
oh! it's literally that
it waits
it yields your once at the end of the frame
oooor
every 0.015 seconds
it's important to do that causeee IF you don't
it'll literally error
cause the while loop will go fucking bonkers
yeah i ran that and it outputted an error
if your computer does not explode. it should give you an exhausted execution time kind of error
oh you did
good
yeah, makes a lot more sense
reason, it tries to run MANY FUCKING TIMES, every frame
LOL
like you know how big the number of executions it has to be for it to crash
that's a HUGE NUMBER
and well remember
when scripts are read by the interpreter they are executed like so, so the while loop will
- evaluate the condition and if it's true
- execute the condition
- go back to the top
- evaluate the condition
so your condition will be evaluated ONCE every heartbeat (frame)
with task.wait
or one second if you use task.wait(1)
you can pass any number really that is bigger than 0.0015
do not confuse with wait()
wait updates every 2 frames
not every frame, so more innacurate, plus some hidden behaviors that fucks your code up
ah, so pretty much use task.wait instead of wait()?
every time, yeah
alright
if you see an example using wait
use task.wait
on the surface they do basically the same thing, but under the hood, yeah they're very different
too nuanced to explain why, maybe in the future
gotcha
aight, you have a good one, again remember you can ping me if you have questions, if I can help I will
but do remember to use scripting help forum, cause my help is obviously not guaranteed
is variable to my availability and your problem.
okay, i will do, you have a good one too and thanks for the help
noo worries!
@tired inlet hey.... i had a question abt some code i got from a scripting tutorial
Part = script.Parent
Part.Touched.connect(function()
X = Part:Clone()
X.Parent = game.Workspace
end)
why does that give me an error and not work but something like this
Part = script.Parent
Part.Touched:connect(function()
X= game.Lighting.Parte:Clone()
X.Parent = game.Workspace
end)
works
(BTW dont mind the bad variable names i just wanted to test it out fast)
no worries man hahaha