My Sliding door doesn't work

Hello guys,
As you can see from the title my sliding door isn’t working properly as it doesn’t close fully.
https://gyazo.com/d823cf9555c5efbfa2f2f7640846a4c8 <<<This shows the door not closing


This is the script.

I’d appreciate any help.

EDIT: I created a repo file so you can reproduce the error yourself and hopefully it will be easier for you to figure out what the issue is. Thanks again for helping me out! ErrorRepo.rbxl (31.4 KB)

3 Likes

Have you tried printing the CFrames after each tween?

Did you get the right CFrame value for when you are closing the door? Like you didn’t make any errors?

Please copy and paste the code in the code format as a reply so I can view it better.

Judging by what I can see, I suggest you use a click-detector, and not a value or whatever you’re using to open it, as you can set the max activation distance to 0 to make it un-clickable while it’s running.
Also, you use a wait() function after the opening sequence, but for the closing sequence you use a function that waits for the sequence to finish. It may be that.
And that code clearly isn’t yours, as it has the comments after the TweenInfo line.
What I would suggest is trying other tutorials if you are following them, as they will have different answers/code to this.
I can’t find a reason to this, as I can’t run it myself.
Try hovering over the code, maybe something is missing? Also, check the console if anything shows up.

1 Like

I literally just realised, you’re trying to move the X axis with a 3 value set. Either change that to 1 value, or remove the .X from the code.

When I remove the .X from the code it just produces an error

Here is the code:

local TweenService = game:GetService("TweenService")

local Door = script.Parent.Door
local DoorRoot = Door.PrimaryPart
local toggle = false

function openDoor()
	if toggle == false then
		toggle = true
		local DoorSlideInfo = TweenInfo.new() -- Let's use all defaults here
		local DoorSlideOpenTween = TweenService:Create(DoorRoot, DoorSlideInfo, {
			CFrame = DoorRoot.CFrame * CFrame.new(DoorRoot.Size.X + 4.5, 0, 0)
		})
		DoorSlideOpenTween:Play()
		wait(4)
		local DoorSlideInfo = TweenInfo.new() -- Let's use all defaults here
		local DoorSlideCloseTween = TweenService:Create(DoorRoot, DoorSlideInfo, {
			CFrame = DoorRoot.CFrame * CFrame.new(DoorRoot.Size.X - 4.5, 0, 0)
		})
		DoorSlideCloseTween:Play()
		DoorSlideCloseTween.Completed:Wait()
		toggle = false
	end
end

Door.ClickDetector.MouseClick:Connect(openDoor)

I’m not so sure on how to do this as I don’t have much experience in scripting. Can you help me out with it?

In the output it returns these two sets of values. The first one is the CFrame of the RootPart after the OpenTween. The second one is the CFrame of the RootPart after the CloseTween.

Replace CFrame = DoorRoot.CFrame * Cframe.new(DoorRoot.Size.X + 4.5, 0, 0) with Vector3 = DoorRoot.CFrame * Vector3.new = DoorRoot.Position(4.5, 0, 0)

I wrote it like that and it shows the red line underneath the = sign next to Vector3.new

hmm what if you change it to
Vector3 = DoorRoot.CFrame * (Vector3.new = DoorRoot.Position(4.5, 0, 0))?

Same problem occurs unfortunately

Well you clearly have to use Vector3…
I don’t really know what else to do…
Have you tried using the PrimaryRootPart for the door model and not the root attached to the door?

I used it as I heard it was the most efficient method of tweening a model according to an article. If you want to see the error for yourself I’ve attached a file where you can reproduce it.

Thank you!

1 Like

I found it out.

Using a part’s size to find out the size of the gap after the first click, I added it to the 4.5 in the closing sequence, and I am fine tuning it right now to make sure there is no gap. I will message again with the correct number.

1 Like

I found it!
Here is the new/correct code.

local TweenService = game:GetService("TweenService")

local Door = script.Parent.Door
local DoorRoot = Door.PrimaryPart
local toggle = false

function openDoor()
	if toggle == false then
		toggle = true
		local DoorSlideInfo = TweenInfo.new()
		local DoorSlideOpenTween = TweenService:Create(DoorRoot, DoorSlideInfo, {
			CFrame = DoorRoot.CFrame * CFrame.new(DoorRoot.Size.X + 4.5, 0, 0)
		})
		DoorSlideOpenTween:Play()
		DoorSlideOpenTween.Completed:Wait()
		print(DoorRoot.CFrame)
		wait(4)
		local DoorSlideInfo = TweenInfo.new()
		local DoorSlideCloseTween = TweenService:Create(DoorRoot, DoorSlideInfo, {
			CFrame = DoorRoot.CFrame * CFrame.new(DoorRoot.Size.X - 4.93, 0, 0) -- Exact number for no gap.
		})
		DoorSlideCloseTween:Play()
		DoorSlideCloseTween.Completed:Wait()
		print(DoorRoot.CFrame)
		toggle = false
	end
end

Door.ClickDetector.MouseClick:Connect(openDoor)
1 Like

Thanks so much! I was struggling to do this for a while :weary: :slightly_smiling_face:

You’re welcome man, ask me again for stuff like this anytime lol. :wink:

1 Like

I know that the matter has been resolved, but I would like to point out that the code uses DoorRoot.Size.X when trying to transpose the door 4.5 studs in a certain direction.

If you wished to transpose the door 4.5 studs in a certain direction, relative to the rotation of the door, then might I suggest using removing the DoorRoot.Size.X from the code altogether and replacing the -4.93 with -4.5.

1 Like