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

How do I make this loop work?

Asked by 8 years ago

It's supposed to check every tenth of a second if it's night (18:00:00 thru 6:15:00) and if each part of a model is either Institutional white or New Yeller. It isn't working and there's no output. Please help!

Here's the script:

while wait(0.1) do
    local Children = script.Parent.ZombieSignA:GetChildren()
    for i,v in pairs(Children) do
        if game.Lighting.TimeOfDay >= "18:00:00" or game.Lighting.TimeOfDay <= "6:15:00" and v.BrickColor == "Institutional white" or v.BrickColor == "New Yeller" then
            v.Material = "Neon"
        else
            v.Material = "Plastic"
        end
    end
end

Thank you in advance for any help you may provide.

0
Im probaly blind but did u make the v a variable i bet im skimming right over it iSvenDerp 233 — 8y
0
for i,v in pairs(Children) do ... you are blind xD Prohibetur 70 — 8y
0
Thought so iSvenDerp 233 — 8y
0
I see it now:3 iSvenDerp 233 — 8y

2 answers

Log in to vote
2
Answered by
Discern 1007 Moderation Voter
8 years ago

TimeOfDay is a string. Therefore, you cannot compare if one is larger than the other, because they are strings, not numbers.

To fix this, you can use the GetMinutesAfterMidnight method to determine the amount of minutes that has passed after midnight. This method takes no parameters. Midnight is 00:00:00, so 18:00:00 would be 1,080 minutes and 6:15:00 would be 375 minutes.

Also, in your conditional statement, the wrong conditions would be met before the script would continue. Place parentheses around the statements you want to count as a whole.

Fixed Code:

while wait(0.1) do
    local Children = script.Parent.ZombieSignA:GetChildren()
    for i,v in pairs(Children) do
        if (game.Lighting:GetMinutesAfterMidnight() >= 1080 or game.Lighting:GetMinutesAfterMidnight() <= 375) and (v.BrickColor == BrickColor.new("Institutional white") or v.BrickColor == BrickColor.new("New Yeller")) then
            v.Material = Enum.Material.Neon
        else
            v.Material = Enum.Material.Plastic
        end
    end
end

If I helped you, be sure to accept my answer!

0
I had to flip the materials for some reason (no problem), but it stops checking after it runs once. SchonATL 15 — 8y
Ad
Log in to vote
2
Answered by 8 years ago
while wait(0.1) do
    local Children = script.Parent.ZombieSignA:GetChildren()
    for i,v in pairs(Children) do
        if game.Lighting:GetMinutesAfterMidnight() >= 1080  or game.Lighting:GetMinutesAfterMidnight() <= 375 and (v.BrickColor == BrickColor.new("Institutional white") or v.BrickColor == BrickColor.new("New Yeller")) then
            v.Material = "Neon"
        else
            v.Material = "Plastic"
        end
    end
end

You cannot compare TImeOfDay as a number because it is a string. But we could use :GetMinutesAfterMidnight(). :GetMinutesAfterMidnight() gets the amount of minutes passed after 24:00:00 or 00:00:00. Obviously, to convert 'TimeOfDay' to ':GetMinutesAfterMidnight()' We will have to convert the hours to minutes. Hence the numbers 1080 and 375.

0
I had to flip the materials for some reason (no problem), but it stops checking after it runs once. SchonATL 15 — 8y

Answer this question