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

Is this the best way to index something revolving all model parts?

Asked by 4 years ago

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.]]
0
I know the script is a mess, I apologize. I just want to know exactly what I can do. If I can do multiple values for the in pairs (values) then please inform me on a way I can learn how to do this please. Just2Terrify 566 — 4y
0
I've been looking and attempting for a while and nothings working. Just2Terrify 566 — 4y
0
FYI again, This script DOES work so I know it's possible. Just2Terrify 566 — 4y

1 answer

Log in to vote
3
Answered by
crywink 419 Moderation Voter
4 years ago

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.

0
Thank you for the upvote! My goal is to have everything in the model, part or not to change transparency and cancollide, hence why I didn't use the :IsA method. Thank you though! How would I iterate through the doors?... Just2Terrify 566 — 4y
1
`for _,v in pairs(doors:GetChildren()) do ... end` crywink 419 — 4y
Ad

Answer this question