Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is my door glitching out?

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

✓ Accepted answer

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)

1 comment

0

Thanks! I completely forgot about for loops :P

This discussion is preserved as it was. New answers, comments, and votes are closed.