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)
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)
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)