Model has collision when using :MoveTo()

Okay so I’m making a sliding door, but every time I activate it, it will teleport the model on top of the building, even though I have CanCollide for every single part off, when being moved. Here’s my code for opening and closing the door.

Open Door Code:

function OpenDoor()
	door.PrimaryPart = door.Glass
	cido.Value = true
	script.Parent.col.BrickColor = acol
	local cframe = door.PrimaryPart.CFrame -- Current CFrame
	
	for i, v in pairs(door:GetChildren()) do
		v.CanCollide = false
	end
	local vz = 0
	for count = 1, 45 do
		vz = vz - 0.007
		local pos = Vector3.new(door.Glass.Position.X, door.Glass.Position.Y, door.Glass.Position.Z + vz)
		door:MoveTo(pos)
		wait(0.0000000000001)
	end
end

Close Door Code:

function CloseDoor()
	door.PrimaryPart = door.Glass
	cido.Value = false
	script.Parent.col.BrickColor = dcol
	local cframe = door.PrimaryPart.CFrame -- Current CFrame
	
	local vz = 0
	for count = 1, 45 do
		vz = vz - 0.007
		local pos = Vector3.new(door.Glass.Position.X, door.Glass.Position.Y, door.Glass.Position.Z - vz)
		door:MoveTo(pos)
		wait(0.0000000000001)
	end
	for i, v in pairs(door:GetChildren()) do
		v.CanCollide = true
	end
end

Thanks for your help! :smile_cat:

5 Likes

I’m not sure if but maybe you will be disappointed when you rotate the door and the Z axis becomes the front and not the side, however I haven’t tested this and I’m not sure so don’t hold my word on it.

The function MoveTo was designed to increase the y axis if there was a part obstructing the space the model was trying to be placed in. You’ll have to use SetPrimaryPartCFrame if you want it to force it in that position.

8 Likes

Also a much better solution would be to use the TweenService to reposition the door parts. Will give you a much smoother animation.

2 Likes

If the model colliding is a problem , then make sure the Primary Part of a model is set, and it’s CanCollide property is set to false.
also as DesiredFlamingFire mentioned, use SetPrimaryPartCframe

This is what I changed my code to and it worked.

This is a good solution, although it is harder to implement with models. I will look more into this.

Thanks for everyone’s help! :smile_cat:

So for model all you have to do is weld/motor6d the parts to their respectable base part.

What would be the difference between them? Is one better than the other?

Motor6Ds are more flexible with CFrame whereas welds aren’t on the same level. However, it entirely depends on what you want to achieve. For tweening a model, it doesn’t matter if you use welds or Motor6Ds unless you plan to add in animations to the model.