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

How can I select multiple parts via scripting ?

Asked by 7 years ago

So today I was trying to make a script where there are white parts (DayBrick) and black parts (NightBrick). I wanted to make a script where on a ScreenGui button click any of the bricks disappear (NOTE: There are mutliple parts named 'NightBrick' and 'DayBrick'). The script is set as a Script and put into Workspace. The goal is that the script must detect all of the parts named each of them and change what they do/are.

Day = game.StarterGui.ScreenGui.DayButton
Night = game.StarterGui.ScreenGui.NightButton
DayBrick = game.Workspace.DayBrick
NightBrick = game.Workspace.NightBrick

Day.MouseButton1Click:connect(function()
    for _,Part in next, (game.Workspace:GetChildren()) do
        if (Part .Name == "DayBrick") and Part :IsA("BasePart")  then
        DayBrick.Transparency = 0
        DayBrick.CanCollide = true
        NightBrick.Transparency = 1
        NightBrick.CanCollide = false
        end
    end
end)

Night.MouseButton1Click:connect(function()
    for _,Part in next, (game.Workspace:GetChildren()) do
        if (Part .Name == "NightBrick") and Part :IsA("BasePart")  then
        DayBrick.Transparency = 1
        DayBrick.CanCollide = false
        NightBrick.Transparency = 0
        NightBrick.CanCollide = true
        end
    end
end)
0
Use game.Selection:Set() hiimgoodpack 2009 — 7y

2 answers

Log in to vote
1
Answered by
Troidit 253 Moderation Voter
7 years ago

You can just use pairs in a for loop. I usually never use next.

Also, I think it'd be a lot easier if you made two seperate folder instances and put the two different types of bricks in each instead of getting all the children of the entire workspace.

Day = game.StarterGui.ScreenGui.DayButton
Night = game.StarterGui.ScreenGui.NightButton
DayFolder = game.Workspace.DayFolder:GetChildren()--day bricks are inside here
NightFolder = game.Workspace.NightFolder:GetChildren()--night bricks are inside here

Day.MouseButton1Click:connect(function()--Notice that I took out the if function because we already can assume any parts inside the day folder are only a day block.
    for key,Part in pairs(DayFolder) do
        Part.Transparency = 0
        Part.CanCollide = true
        Part.Transparency = 1
        Part.CanCollide = false
    end
end)

Night.MouseButton1Click:connect(function() --Notice that I took out the if function because we already can assume any parts inside the night folder are only a night block.
    for key,Part in pairs(NightFolder) do 
        Part.Transparency = 1
        Part.CanCollide = false
        Part.Transparency = 0
        Part.CanCollide = true
    end
end)
0
pairs vs next is just preference, really CootKitty 311 — 7y
0
next is like the easiest function to recreate ever, just do table[num + 1] hiimgoodpack 2009 — 7y
0
I get that it's preference, I just posted what I normally would do. Troidit 253 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

Thank you for the script,I made slight changes (the time), but I found that when I click on 'Day' then the DayBlocks disappear, click on 'Night' - nothing happens + on daytime, the night blocks disappear and on nighttime, the dayblocks disappear, just to make it more correct. (It's a puzzle-adventure game I'm making)

Day = script.Parent.DayButton
Night = script.Parent.NightButton
DayFolder = game.Workspace.DayFolder:GetChildren()
NightFolder = game.Workspace.NightFolder:GetChildren()

Day.MouseButton1Click:connect(function()
    for key,Part in pairs(DayFolder) do
        Part.Transparency = 0   --Why is there 4 rows of 'Part.' ?
        Part.CanCollide = true  --Same
        Part.Transparency = 1   --Same
        Part.CanCollide = false     --Same
        game.Lighting.TimeOfDay = '14:00:00'
    end
end)

Night.MouseButton1Click:connect(function()
    for key,Part in pairs(NightFolder) do 
        Part.Transparency = 1
        Part.CanCollide = false
        Part.Transparency = 0
        Part.CanCollide = true
        game.Lighting.TimeOfDay = '00:00:00'
    end
end)
0
There are four rows of part because in the for loop, the variable Part is represented as each brick going down the list. Troidit 253 — 7y

Answer this question