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

Help with an onClick function that moves parts?

Asked by
Jiptix 2
6 years ago

So I am trying to make a gate with two parts. The left part moves left when activated, and the right part moves right when activated. Once the parts are done separating, you can click the activation part again to bring them back together. They are coded to the same activation part. (Clickable button.) For some reason, the right part works fine, but the left part stops moving before it should - way before the right part stops moving.

local trigger = script.Parent
local gate = script.Parent.Parent.GateRight

moving = false
open = false

function onClick()

    if open == false and moving == false then

        moving = true
        trigger.BrickColor = BrickColor.new("Bright yellow")

        for i = 1, (gate.Size.Z * 10 + 1) do
            wait()
            gate.CFrame = gate.CFrame*CFrame.new(0, 0, 0.0875)
        end

        moving = false
        open = true
        trigger.BrickColor = BrickColor.new("Bright red")

    elseif open == true and moving == false then

        moving = true
        trigger.BrickColor = BrickColor.new("Bright yellow")

        for i = 1, (gate.Size.Z * 10 + 1) do
            wait()
            gate.CFrame = gate.CFrame*CFrame.new(0, 0, -0.0875)
        end

        moving = false
        open = false
        trigger.BrickColor = BrickColor.new("Bright green")

    end

end

script.Parent.ClickDetector.MouseClick:connect(onClick)

^ This code is for moving the right part.

local trigger = script.Parent
local gate = script.Parent.Parent.GateLeft

moving = false
open = false

function onClick()

    if open == false and moving == false then

        moving = true
        trigger.BrickColor = BrickColor.new("Bright yellow")

        for i = 1, (gate.Size.Z * 10 + 1) do
            wait()
            gate.CFrame = gate.CFrame*CFrame.new(0, 0, -0.0875)
        end

        moving = false
        open = true
        trigger.BrickColor = BrickColor.new("Bright red")

    elseif open == true and moving == false then

        moving = true
        trigger.BrickColor = BrickColor.new("Bright yellow")

        for i = 1, (gate.Size.Z * 10 + 1) do
            wait()
            gate.CFrame = gate.CFrame*CFrame.new(0, 0, 0.0875)
        end

        moving = false
        open = false
        trigger.BrickColor = BrickColor.new("Bright green")

    end

end

script.Parent.ClickDetector.MouseClick:connect(onClick)

^This is the code for the left part.

I have no idea why this won't work? Please help.

Answer this question