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

The for loop to change the material doesn't work. Help?

Hey guys!

So I want a building to change the materials for it's windows according to the time and for some reason this doesn't work. Help?

Code:

local light = script.Parent.Model:GetChildren()

for i = 1, #light do
	wait(0.1)
	if game.Lighting:GetMinutesAfterMidnight() > 6 * 60 then
		light[i].Material = Enum.Material.Plastic
	end
	if game.Lighting:GetMinutesAfterMidnight() > 18 * 60 then
		light[i].Material = Enum.Material.Neon
	end
end

2 comments

0

Try droppin a line between the L and end. Might be the issue, or its because Model or Enum is in capitals, Reply back if this helps!

0

What is the length of light variable? if it is equal to 1, then the loop wont run

2 answers

✓ Accepted answer
lightPart = workspace.lights:GetChildren() -- Table containing all of the lights
 
function changeMaterial(material)
    for i, v in pairs(lightPart) do -- Iterates through all the lights
        v.Material = material  -- Sets each light's material
    end
end
 
game.Lighting.Changed:connect(function(property) -- This will fire whenever the time changes, so that you don't have to use a 'while true do' loop
    if game.Lighting:GetMinutesAfterMidnight() == 6 * 60 then
        changeMaterial(Enum.Material.Plastic) -- Calls function to change light's material
    end
    if game.Lighting:GetMinutesAfterMidnight() == 18 * 60 then
        changeMaterial(Enum.Material.Neon) -- Calls function to change light's material
    end
end)

Just change lightPart's location with wherever your Model with the lights is.

2 comments

0

((you should also place the script into serverscriptstorage))

0

Thanks, it worked :D

local light = script.Parent.Model:GetChildren()
--if this doesn't work, then it has to do something with:  if game.Lighting:GetMinutesAfterMidnight() > 6 * 60

for i = 1, #light do
    wait(0.1)
    if game.Lighting:GetMinutesAfterMidnight() > 6 * 60 then 
	if light[i]:IsA('BasePart') then -- detects if it is a part
       	 light[i].Material = Enum.Material.Plastic
	end
    end
    if game.Lighting:GetMinutesAfterMidnight() > 18 * 60 then
       if light[i]:IsA('BasePart') then 
       	 light[i].Material = Enum.Material.Neon
	end
    end
end

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