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

Why is my door glitching out?

Asked by 10 years ago

I have a door that is supposed to open if you are on the right team. It goes up and down alright the first time, but the second time, it goes up to your head height then rapidly bounces up and down 1 stud until you exit the door. When you exit the door, it closes like it should, but it glitches out again when you open it. If you want to see it in action, you can look at it here by touching the dark wall. Also note that the door is anchored. Here is the code:

local Door = script.Parent
local Color = "New Yeller"
local DB = false
function Open()
    print("Opening")
    while Door.Position.Y < 98.5 do
        Door.Position = Door.Position + Vector3.new(0, 0.75, 0)
        wait(0.05)
    end
end
function Close()
    print("Closing")
    while Door.Position.Y > 83.5 do
        Door.Position = Door.Position - Vector3.new(0, 0.75, 0)
        wait(0.05)
    end
end
function onTouch(part)
    if game.Players:GetPlayerFromCharacter(part.Parent) and DB == false then
        local Player = game.Players:GetPlayerFromCharacter(part.Parent) 
        if Player.TeamColor == BrickColor.new(Color) then
            DB = true
            Open()
            wait(2)
            Close()
            wait(2)
            DB = false
        end
    end
end
Door.Touched:connect(onTouch)

Any Suggestions?

1 answer

Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
10 years ago

You may use a 'for' loop instead of a 'while' loop. You may also use a CFrame property instead of the Position property, as it does not have a built-in collision checker.

-- Feel free to make adjustments to this script.
Door = script.Parent
Color = "New Yeller"
DB = false

function Open()
    for i = 1, 10 do
        Door.CFrame = Door.CFrame * CFrame.new(0, 1, 0)
        wait(.05)
    end
end

function Close()
    for i = 1, 10 do
        Door.CFrame = Door.CFrame * CFrame.new(0, -1, 0)
        wait(.05)
    end
end

function onTouch(part)
    if game.Players:GetPlayerFromCharacter(part.Parent) and DB == false then
        local Player = game.Players:GetPlayerFromCharacter(part.Parent)
        if Player.TeamColor == BrickColor.new(Color) then
            DB = true
            Open()
            wait(2)
            Close()
            wait(2)
            DB = false
        end
    end
end

Door.Touched:connect(onTouch)
0
Thanks! I completely forgot about for loops :P User#348 0 — 10y
Ad

Answer this question