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

Looping through two parts to get children?

I'm following the simple day/night script from the ROBLOX Wiki (http://wiki.roblox.com/index.php?title=Making_a_Day/Night_Cycle). I thought, as a challenge, was to have two parts in a lamp that gave off a light and a single script would handle how the lights behaved in both of the parts.

local minutesAfterMidnight = 0
local lightParts = game.Workspace.Lamp.Light:GetChildren() -- this would get the parts containing Point Lights

for i, v in ipairs(lightParts) do -- looping through each part
	lights = {} -- table to contain ACTUAL point lights
	table.insert(lights, v:GetChildren()) -- insert the two Point lights into the the 'lights list'
	for j, k in ipairs(lights) do -- loop through lights
		if game.Lighting.TimeOfDay == "06:00:00" then -- check if it is 6:00AM
			k.Parent.Material = Enum.Material.Plastic
			k.PointLight.Enabled = false -- turn off lights
		end
	end
end

while true do
	minutesAfterMidnight = minutesAfterMidnight + 1
	game.Lighting:SetMinutesAfterMidnight(minutesAfterMidnight)
	wait(.1)
end

The logic doesn't seem to be adding up and the lights won't turn off when it's 6:00AM. I tried putting in a print statement to print the values of the 'lights' list but it was empty! Even though I inserted the lights there right?

1 comment

0

That is because your script never checks for the TimeOfDay again after it has run once. Tranform the for loops into a function, and call it every time the while loop runs.

1 answer

✓ Accepted answer

###Problems

You are only going through the for loop once, therefore the lights will either be on or off, depending on the initial time, and will never change.

###Solution

We need to make a function that we can call to toggle the lights to be either on or off. We could do this every iteration of the while loop, but that would be inefficient. It would be much better to only toggle the lights when necessary.

Assuming the table of lights is already defined:

local minutesAfterMidnight = 0
local lights = {}

local function toggleLights(boolean)
	if boolean then
		for _, light in next, lights do
			-- Turn on lights
		end
	else
		for _, light in next, lights do
			-- Turn off lights
		end
	end
end

while wait(0.1) do
	minutesAfterMidnight = (minutesAfterMidnight + 1)40 -- 1440 minutes in a day, therefor reset back to 0 when 1440 is reached
	game.Lighting:SetMinutesAfterMidnight(minutesAfterMidnight)
	if minutesAfterMidnight == 360 then -- 360 minutes = 6 hours (6:00 AM)
		toggleLights(false)
	elseif minutesAfterMidnight == 1080 then -- 1080 minutes = 18 hours (6:00 PM)
		toggleLights(true)
	end
end

2 comments

0

I think I get it now. I thought the problem was how I defined the lights (I though there was a simpler way that to table.insert :getChildren()) Also, what is 'next', is it like a placeholder?

0

next is a built in iterator function and is actually what is used in the function pairs. You can use whichever is most comfortable for you to use.

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