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 6 years ago
Edited 6 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:

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
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 — 6y
0
What is the length of light variable? if it is equal to 1, then the loop wont run allecoo1 0 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago
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.

0
((you should also place the script into serverscriptstorage)) radusavin366 617 — 6y
0
Thanks, it worked :D bruceywayne 35 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
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

Answer this question