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

How do I make this loop work?

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.

4 comments

0

Im probaly blind but did u make the v a variable i bet im skimming right over it

0

for i,v in pairs(Children) do ... you are blind xD

0

Thought so

0

I see it now:3

2 answers

✓ Accepted answer

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!

1 comment

0

I had to flip the materials for some reason (no problem), but it stops checking after it runs once.

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.

1 comment

0

I had to flip the materials for some reason (no problem), but it stops checking after it runs once.

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