Alright so this is ran through two GUI buttons and a RemoteEvent. It works perfectly fine. My main issue is I feel like there's an easier way to do this.
What the script is suppose to do.
The script checks to see if SteelCage is a model in workspace and if it is then it continues the code. if the function Open is ran, it turns all of door1's parts transparency to 1 and turns off CanCollide then turns all of door2's parts transparency to 0 and turns on CanCollide. elseif the function is Closed, it turns the door1 parts transparency to 0 and turns on CanCollide then turns all of door2s parts transparency to 1 and CanCollide off. | Yes, the variables named door1 and door2 have to be inside the code script sadly so it doesn't break the entire script as I move this item from ServerStorage to Workspace.
My Question.
Is there a way to make this easier or look more clean? Maybe even move it down a button?
--Made By MillerrIAm ------------------Variables---------------- Event = game.ReplicatedStorage.ExtraEvents.CageDoorEvent ------------------Main Script------------------ Event.OnServerEvent:Connect(function(Player,Door) if game.Workspace:FindFirstChild("SteelCage") then if Door == "Open" then local door1 = game.Workspace.SteelCage.Doors.CageDoor1:GetChildren() local door2 = game.Workspace.SteelCage.Doors.CageDoor2:GetChildren() for i, x in pairs(door1) do x.Transparency = 1 x.CanCollide = false end for i, v in pairs(door2) do v.Transparency = 0 v.CanCollide = true end elseif Door == "Close" then local door1 = game.Workspace.SteelCage.Doors.CageDoor1:GetChildren() local door2 = game.Workspace.SteelCage.Doors.CageDoor2:GetChildren() for i, x in pairs(door1) do x.Transparency = 0 x.CanCollide = true end for i, v in pairs(door2) do v.Transparency = 1 v.CanCollide = false end end else end end) --[[if Door == "Open" then Get all of the children of CageDoor1 & CageDoor2 Turn all of Door1's parts Transparency to 1 and turn CanCollide off then turn all of Door2's parts Transparency to 0 and cancollide on. elseif Door == "Close" then Get all of the children of CageDoor1 & CageDoor2 Turn all of Door2's parts Transparency to 1 and turn CanCollide off then turn all of Door1's parts Transparency to 0 and cancollide on.]]
I gave you an upvote for doing a great job at being descriptive with your question, you don't see that a lot.
Overall your code looks pretty good. If I were you I would use the :IsA
method to make sure the current index is a BasePart and you don't accidentally try to change the transparency of something else, causing your code to break.
Additionally, instead of defining Door1 and Door2, you can iterate through the Doors directory in-case you wanted to add more doors in the future.