Sliding Door with Keycard

I was trying to make a sliding door with a keycard, and it works, however, when the door opens, it keeps turning.

Also, I made a system so when the door is opening, it shows a green rectangle on the keycard handle, and when the room is taken/the door is closing, it shows a red rectangle and when the room is available it shows a white rectangle, and it is working.

The script I used:

taken = false
open = false

local TweenService = game:GetService("TweenService")
local door1 = script.Parent.Parent:WaitForChild("Door1")
local tweeningInformation = TweenInfo.new(
	 0.5,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)
local door1Open = {CFrame = CFrame.new(78.206, 29.55, -107.541)}
local door1Close = {CFrame = CFrame.new(78.206, 29.55, -101.091)}
local tween1open = TweenService:Create(door1,tweeningInformation,door1Open)
local tween1close = TweenService:Create(door1,tweeningInformation,door1Close)

script.Parent.Touched:Connect(function(hit)
	if hit.Parent.Name == "Keycard" and taken == false and open == false then
    open = true
    script.parent.Parent.Body.Color.BrickColor = BrickColor.new("Olivine")
    tween1open:Play()
	wait(2.5)
    script.parent.Parent.Body.Color.BrickColor = BrickColor.new("Persimmon")	
    tween1close:Play()
    taken = true
    open = false
    elseif hit.Parent.Name == "Keycard" and taken == true and open == false then
    script.parent.Parent.Body.Color.BrickColor = BrickColor.new("Institutional white")
    taken = false
    open = false
end
end)

Here is the video: https://gyazo.com/ce7a68067a2ef6073d6c3fb824834971 (The video lagged in the start but it was the same as when the door closed but it turned too).

1 Like

You need to set the orientation of the door.

I tried doing that, but it broke the script. I don’t know how to set the orientation properly.

This is a helpful video.

1 Like

Perhaps set the CFrame of the door relative to it’s original CFrame. What I mean is, record the original CFrame of the door, and use that CFrame to move the door accordingly.

open = false

local TweenService = game:GetService("TweenService")
local door1 = script.Parent.Parent:WaitForChild("Door1")
local tweeningInformation = TweenInfo.new(
	 0.5,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)
local originalCFrame = door1.CFrame
local door1Open = {CFrame = originalCFrame * CFrame.new(0, 0, -6.5)}
local door1Close = {CFrame = originalCFrame)}
local tween1open = TweenService:Create(door1,tweeningInformation,door1Open)
local tween1close = TweenService:Create(door1,tweeningInformation,door1Close)

script.Parent.Touched:Connect(function(hit)
    if hit.Parent.Name == "Keycard" and taken == false and open == false then
       open = true
       script.parent.Parent.Body.Color.BrickColor = BrickColor.new("Olivine")
       tween1open:Play()
       wait(2.5)
       script.parent.Parent.Body.Color.BrickColor = BrickColor.new("Persimmon")	
       tween1close:Play()
       taken = true
       open = false
    elseif hit.Parent.Name == "Keycard" and taken == true and open == false then
       script.parent.Parent.Body.Color.BrickColor = BrickColor.new("Institutional white")
       taken = false
       open = false
   end
end)

CFrame is a matrix that accounts for horizontal, vertical, and rotational position. You only had 3 values listed in your CFrame, which is probably why rotation wasn’t accounted for. Using this method, the original CFrame is recorded with the actual position, and now you’re just moving the Door 6.5 studs in the negative Z Direction with the rotation accounted for.

2 Likes

That could also work, it really depends on your style of coding. I personally like to be precise with CFrames using orientation but you could also get away with using the original CFrame.

Rotation is accounted for in the CFrame since it’s a matrix.

Well there are lots of different CFrames and the version they used only includes the position. It’s really an opinion on how you like to do stuff but that isn’t the topic of this question.

There was an extra parenthesis, and I had to change the order in CFrame.new(0, 0, -6.5) because it was going forwards instead of sideways, but it worked. Thank you. qwq