Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Zesuq 3
4 years ago

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

Log in to vote
3
Answered by 4 years ago
Edited 4 years 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 :

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!
0
and also don't use while wait if your cool imKirda 4491 — 4y
0
Well, there's no disadvantage of using while loop. BestCreativeBoy 1395 — 4y
0
i'm talking about while wait() instead of while true.. wait ^^ imKirda 4491 — 4y
1
NVM I suggested him :GetpropertyChangedSignal() BestCreativeBoy 1395 — 4y
1
@imKirda and @BestCreativeBoy, I sincerely thank you for you have saved my butt. Keep being awesome! Zesuq 3 — 4y
Ad

Answer this question