Slide door problem

I’m Have little problem, when i opening my sideing door like 3-4 times they becoming crazy.robloxapp-20210818-2151431.wmv (1.8 MB)

x = script.Parent.Door.Position.X
y = script.Parent.Door.Position.Y
z = script.Parent.Door.Position.Z

ySize = script.Parent.Door.Size.Y / 2

workspace.Button.ClickDetector.MouseClick:Connect(function()

for i = y, -ySize, -0.1 do
	script.Parent.Door.Position = Vector3.new(x, i, z)
	wait(0.01)
	print(ySize)
end

wait(1)

for i = -y, ySize, 0.1 do
	script.Parent.Door.Position = Vector3.new(x, i, z)
	wait(0.01)
	print(ySize)
end

end)

You should add a debounce. In the video, you’re trying to open the door while it’s already opening/closing. This causes the functions to conflict as they couldn’t agree on what position the door should be.

Also, you should consider using TweenService for the door animation instead of running for loops.

Try this:

x = script.Parent.Door.Position.X
y = script.Parent.Door.Position.Y
z = script.Parent.Door.Position.Z

ySize = script.Parent.Door.Size.Y / 2

local moving = false
workspace.Button.ClickDetector.MouseClick:Connect(function()
	if not moving then --if the door isn't already moving then animate the door
		moving = true --set "moving" to true so that future calls to open the door will be blocked
		for i = y, -ySize, -0.1 do
			script.Parent.Door.Position = Vector3.new(x, i, z)
			wait(0.01)
			print(ySize)
		end
		
		wait(1)
		
		for i = -y, ySize, 0.1 do
			script.Parent.Door.Position = Vector3.new(x, i, z)
			wait(0.01)
			print(ySize)
		end
		moving = false --set "moving" to false as now the door is done animating and is no longer moving
	end
end)
1 Like

Ok, i’ll test it. Thanks for help!

robloxapp-20210818-2231108.wmv (1.5 MB) Works!