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

How do I move multiple parts in the same direction with one script/function?

Asked by 7 years ago

I am trying to create a sliding door made of multiple parts all moving in the same direction. CSG is not an option in this case.

Now, with all of that in mind, I have a script inside a model. The model contains a button, (named "Button"), the "MainDoor" Part, as well as three other parts called "TopDoor." There is also a BoolValue called "IsClosed" that I am using as a switch to keep the doors open and closed.

How should I convert the below script to include the other parts being moved exactly 6.8 units up along the Y axis, keeping them "attached" to the "MainDoor" part?

Code:

door = script.Parent.MainDoor
open = script.Parent.IsClosed
debounce = true

function MoveDoor()
    if debounce == true and open.Value then
        open.Value = false
            debounce = false
            for i = 1, 68 do
            door.CFrame = door.CFrame + Vector3.new(0, 0.1, 0)
            wait()
            end
            debounce = true
        else
    if debounce == true then
        open.Value = true
            debounce = false
            for i = 1, 68 do
            door.CFrame = door.CFrame + Vector3.new(0, -0.1, 0)
            wait()
            end
            debounce = true
        end 
    end 
end

debounce = true

script.Parent.Button.ClickDetector.MouseClick:connect(MoveDoor)

1 answer

Log in to vote
1
Answered by
Yionee 65
7 years ago

With your current setup, here's (hopefully) a fix:

local door = script.Parent.MainDoor --local variables are faster
local open = script.Parent.IsClosed
local debounce = true

function MoverFunction(ychange) --function to reduce repetitive code
    for i = 1, 68 do
        for _,Object in pairs(script.Parent:GetChildren()) do
            if Object.Name == "MainDoor" or Object.Name == "TopDoor" then
                Object.CFrame = Object.CFrame + Vector3.new(0, ychange, 0)
            end
        end
    end
    debounce = true --moved here to save 1 line :p
end

function MoveDoor()
    if debounce and open.Value then
        open.Value, debounce = false, false
        MoverFunction(0.1) --y value change for each increment
    elseif debounce and not open.Value then --removed the extra if statement
        open.Value, debounce = true, false
        MoverFunction(-0.1)
    end 
end 

script.Parent.Button.ClickDetector.MouseClick:connect(MoveDoor)
0
Works beautifully, except for a minor flaw that I can fix. Thank you! DarkSideKyp 50 — 7y
Ad

Answer this question