This works
01 | local gametime = game.Lighting.ClockTime |
02 |
03 | while wait( 1 ) do |
04 | if (game.Lighting.ClockTime > 6.3 ) and (game.Lighting.ClockTime < 17.3 ) then |
05 | window.Color = Color 3. new( 0.231373 , 0.603922 , 0.635294 ) |
06 | window.Material = Enum.Material.SmoothPlastic |
07 | else |
08 | window.Color = Color 3. new( 0.741176 , 0.698039 , 0.192157 ) |
09 | window.Material = Enum.Material.Neon |
10 | end |
11 | end |
But this doesn't
01 | local window = game.Workspace.Window |
02 | local gametime = game.Lighting.ClockTime |
03 |
04 | while wait( 1 ) do |
05 | if (gametime > 6.3 ) and (gametime < 17.3 ) then |
06 | window.Color = Color 3. new( 0.231373 , 0.603922 , 0.635294 ) |
07 | window.Material = Enum.Material.SmoothPlastic |
08 | else |
09 | window.Color = Color 3. new( 0.741176 , 0.698039 , 0.192157 ) |
10 | window.Material = Enum.Material.Neon |
11 | end |
12 | end |
======================== Can someone explain why it won't work with a variable?
If possible super simply, I just started scripting a few days ago!
Well, the problem, here, is that you are declaring a variable for the ClockTime
when the game loads, i.e., 14
[Default]. So, your variable will remain fixed to 14
[Default] only and it won't change as there is no Function
or Event
to trigger/update the variable again.
So, what's the fix?
Well, declare your variable inside the while
loop to get it updated everytime. This is how your code must look :
01 | local window = game.Workspace.Window |
02 |
03 | while wait( 1 ) do |
04 | local gametime = game.Lighting.ClockTime |
05 |
06 | if (gametime > 6.3 ) and (gametime < 17.3 ) then |
07 | window.Color = Color 3. new( 0.231373 , 0.603922 , 0.635294 ) |
08 | window.Material = Enum.Material.SmoothPlastic |
09 | else |
10 | window.Color = Color 3. new( 0.741176 , 0.698039 , 0.192157 ) |
11 | window.Material = Enum.Material.Neon |
12 | end |
13 | end |
Lemme know if it helps!
EDIT : You can use GetPropertyChangedSignal
instead of while
loop, to reduce memory usage/stress.
01 | --// Variables |
02 | local Lightning = game:GetService( 'Lightning' ) |
03 | local window = workspace.Window |
04 |
05 | --// Functions |
06 | local function onChanged() |
07 | local gametime = Lightning.ClockTime |
08 |
09 | if (gametime > 6.3 ) and (gametime < 17.3 ) then |
10 | window.Color = Color 3. new( 0.231373 , 0.603922 , 0.635294 ) |
11 | window.Material = Enum.Material.SmoothPlastic |
12 | else |
13 | window.Color = Color 3. new( 0.741176 , 0.698039 , 0.192157 ) |
14 | window.Material = Enum.Material.Neon |
15 | end |
16 | end |
17 |
18 | --// Main |
19 | Lightning:GetPropertyChangedSignal( 'ClockTime' ):Connect(onChanged) -- Triggers everytime whenever the 'ClockTime' value is changed! |