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

My NeonWindow script isnt searching through all descendants?

Asked by 7 years ago

This is my glass window script, it searches for all the parts called NeonWindow and then turns it from glass to Neon material at 19:00:00, and then Neon to glass at 07:00:00. Not ever NeonWindow part does this...I'm assuming this is because I'm not searching through all the descendants.

How would I do this, and if my assumption is wrong what else should I do instead.

while wait() do
    if game.Lighting.TimeOfDay > "19:00:00" then

        for _, glass in pairs (game.Workspace:GetChildren()) do

            if glass.Name == "NeonWindow" then
                glass.Material = 288

            elseif glass.Name ~= "NeonWindow" then

                for _, glass2 in pairs (glass:GetChildren()) do

                    if glass2.Name == "NeonWindow" then
                        glass2.Material = 288

                    end
                end
            end
        end 
    elseif game.Lighting.TimeOfDay "07:00:00" then
        for _, glass in pairs (game.Workspace:GetChildren()) do

            if glass.Name == "NeonWindow" then
                glass.Material = 1536

            elseif glass.Name ~= "NeonWindow" then

                for _, glass2 in pairs (glass:GetChildren()) do

                    if glass2.Name == "NeonWindow" then
                    glass2.Material = 1536

                    end
                end
            end
        end 
    end 
end

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

I'm terrible at explaining, but this will cycle through all children and do stuff based on their name. Hope this works. :)

function checkDecendants(parent, enabled)
    for i, v in pairs(parent:GetChildren()) do
        if #v:GetChildren() > 0 then
            checkDecendants(v)
        end

        if v.Name == "NeonWindow" then
            if enabled then
                v.Material = 288
            else
                v.Material = 1536
            end
        end
    end
end

local windowSet = game.Workspace 

game.Lighting.Changed:connect(function()
    if game.Lighting.TimeOfDay == "19:00:00" then
        checkDecendants(windowSet, true)
    elseif game.Lighting.TimeOfDay == "07:00:00" then
        checkDecendants(windowSet, false)
    end
end)
Ad

Answer this question