#Day and night lamp

129 messages · Page 1 of 1 (latest)

twin gate
#

hey, im a very beginner aiming to be scripter and i am trying to figure out how to make a lamp that turns off when it is day, and turns on when it is night, my code doesnt seem to be functioning at all.. could anyone help me?

#

dm me

#

i have code but it does not work

#

i am balding trying to develop a solution for this

fluid grove
#

@twin gate

twin gate
#

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

glass crowBOT
#

studio** You are now Level 1! **studio

twin gate
#

why doesnt taht work

twin gate
#

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

cyan swift
#
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
tired inlet
#

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

tired inlet
# twin gate hey, im a very beginner aiming to be scripter and i am trying to figure out how ...

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

Learn with documentation and resources for all creators.

#

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

twin gate
# tired inlet the code might look like this ```lua local Lighting = game:GetService("Lighting"...

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! :))

tired inlet
#

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

tired inlet
#

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.

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

  2. 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!

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

twin gate
#

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

tired inlet
#
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
tired inlet
#

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

twin gate
#

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

tired inlet
#

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 sunglas

twin gate
#

ah

#

alright

tired inlet
#

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

tired inlet
twin gate
#

@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!!

tired inlet
#

cause illogical code does not really run

twin gate
#

ah that makes more sense

#

that really did help me a lot though

tired inlet
#

it can simply fail to do what you expect

#

or quietly fail

twin gate
#

yeah

tired inlet
#

I'm glad my explanation was useful btw

twin gate
#

wait before u go

tired inlet
#

feel free to ping me if you have more questions

#

I don't mind from you

twin gate
#

could u explain to me one last thing if possible

tired inlet
#

depends

#

I have 15 minutes

twin gate
#

alright

tired inlet
#

so if I can't now I'll do it later

#

so shoot

twin gate
#

whats task.wait() and how does it help with running a while loop

glass crowBOT
#

studio** You are now Level 2! **studio

twin gate
#

dont know if thats the right way to ask it

tired inlet
#

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

twin gate
#

ohhhhhhhh

#

that makes so much more sense

#

thank you

tired inlet
#
while true do
  print(true)
end
#

run that

#

your studio will probably crash

twin gate
#

yeah i ran that and it outputted an error

tired inlet
#

if your computer does not explode. it should give you an exhausted execution time kind of error

#

oh you did

#

good

twin gate
#

yeah, makes a lot more sense

tired inlet
#

reason, it tries to run MANY FUCKING TIMES, every frame

twin gate
#

LOL

tired inlet
#

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

twin gate
#

ah, so pretty much use task.wait instead of wait()?

tired inlet
#

every time, yeah

twin gate
#

alright

tired inlet
#

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

twin gate
#

gotcha

tired inlet
# twin gate 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.

twin gate
#

okay, i will do, you have a good one too and thanks for the help

twin gate
#

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

twin gate
#

wow i just realized why

#

apologies.

tired inlet