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

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

Asked by 7 years ago
Edited 7 years ago

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:

01local light = script.Parent.Model:GetChildren()
02 
03for i = 1, #light do
04    wait(0.1)
05    if game.Lighting:GetMinutesAfterMidnight() > 6 * 60 then
06        light[i].Material = Enum.Material.Plastic
07    end
08    if game.Lighting:GetMinutesAfterMidnight() > 18 * 60 then
09        light[i].Material = Enum.Material.Neon
10    end
11end
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! SniperXShots -5 — 7y
0
What is the length of light variable? if it is equal to 1, then the loop wont run allecoo1 0 — 7y

2 answers

Log in to vote
1
Answered by 7 years ago
01lightPart = workspace.lights:GetChildren() -- Table containing all of the lights
02 
03function changeMaterial(material)
04    for i, v in pairs(lightPart) do -- Iterates through all the lights
05        v.Material = material  -- Sets each light's material
06    end
07end
08 
09game.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
10    if game.Lighting:GetMinutesAfterMidnight() == 6 * 60 then
11        changeMaterial(Enum.Material.Plastic) -- Calls function to change light's material
12    end
13    if game.Lighting:GetMinutesAfterMidnight() == 18 * 60 then
14        changeMaterial(Enum.Material.Neon) -- Calls function to change light's material
15    end
16end)

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

0
((you should also place the script into serverscriptstorage)) radusavin366 617 — 7y
0
Thanks, it worked :D bruceywayne 35 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
01local light = script.Parent.Model:GetChildren()
02--if this doesn't work, then it has to do something with:  if game.Lighting:GetMinutesAfterMidnight() > 6 * 60
03 
04for i = 1, #light do
05    wait(0.1)
06    if game.Lighting:GetMinutesAfterMidnight() > 6 * 60 then
07    if light[i]:IsA('BasePart') then -- detects if it is a part
08         light[i].Material = Enum.Material.Plastic
09    end
10    end
11    if game.Lighting:GetMinutesAfterMidnight() > 18 * 60 then
12       if light[i]:IsA('BasePart') then
13         light[i].Material = Enum.Material.Neon
14    end
15    end
16end

Answer this question