Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Confused why this script won't work with a variable?

This works

local gametime = game.Lighting.ClockTime

while wait(1)do
    if(game.Lighting.ClockTime > 6.3)and(game.Lighting.ClockTime < 17.3)then
        window.Color = Color3.new(0.231373, 0.603922, 0.635294)
        window.Material = Enum.Material.SmoothPlastic
    else
        window.Color = Color3.new(0.741176, 0.698039, 0.192157)
        window.Material = Enum.Material.Neon
    end
end

But this doesn't

local window = game.Workspace.Window
local gametime = game.Lighting.ClockTime

while wait(1)do
    if(gametime > 6.3)and(gametime < 17.3)then
        window.Color = Color3.new(0.231373, 0.603922, 0.635294)
        window.Material = Enum.Material.SmoothPlastic
    else
        window.Color = Color3.new(0.741176, 0.698039, 0.192157)
        window.Material = Enum.Material.Neon
    end
end

======================== Can someone explain why it won't work with a variable?


If possible super simply, I just started scripting a few days ago!


1 answer

✓ Accepted answer

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 :

local window = game.Workspace.Window

while wait(1)do
	local gametime = game.Lighting.ClockTime

    if(gametime > 6.3)and(gametime < 17.3)then
        window.Color = Color3.new(0.231373, 0.603922, 0.635294)
        window.Material = Enum.Material.SmoothPlastic
    else
        window.Color = Color3.new(0.741176, 0.698039, 0.192157)
        window.Material = Enum.Material.Neon
    end
end

Lemme know if it helps!

EDIT : You can use GetPropertyChangedSignal instead of while loop, to reduce memory usage/stress.

--// Variables
local Lightning = game:GetService('Lightning')
local window = workspace.Window

--// Functions
local function onChanged()
	local gametime = Lightning.ClockTime

	if (gametime > 6.3) and (gametime < 17.3) then
        	window.Color = Color3.new(0.231373, 0.603922, 0.635294)
        	window.Material = Enum.Material.SmoothPlastic
    	else
        	window.Color = Color3.new(0.741176, 0.698039, 0.192157)
        	window.Material = Enum.Material.Neon
    	end
end

--// Main
Lightning:GetPropertyChangedSignal('ClockTime'):Connect(onChanged) -- Triggers everytime whenever the 'ClockTime' value is changed!

5 comments

0

and also don't use while wait if your cool

0

Well, there's no disadvantage of using while loop.

0

i'm talking about while wait() instead of while true.. wait ^^

1

NVM I suggested him :GetpropertyChangedSignal()

1

@imKirda and @BestCreativeBoy, I sincerely thank you for you have saved my butt. Keep being awesome!

This discussion is preserved as it was. New answers, comments, and votes are closed.