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

How do I make a sliding door using Vector3?

So I know basic scripting. The thing is, I have 2 scripts. One of the opens and doesn't close, and the other doesn't move. How would I edit the following so that it closes after 2 seconds? Failing this, can you suggest a new script that works in this manner.

local Part = script.Parent
local newPos = Part.Position + Vector3.new(7,0,0)
local Time = 5
local Increment = 0.1
local Debounce = false
 
local Diff = newPos - Part.Position
local Mag = Diff.magnitude
local Direction = CFrame.new(Part.Position, newPos).lookVector
 
function MovePart() 
	if Debounce then return end
	Debounce = true
	for n = 0, Mag, Increment do
		Part.CFrame = Part.CFrame + (Direction * Increment)
		wait( (Time/Mag) * Increment )
	end
	Debounce = false
end
 
script.Parent.Touched:connect(MovePart)

Currently, when you first touch it, it moves to where I want it to. I don't know how to make it go back using the same form of algorithm

Thanks FaultyMatrix

1 answer

✓ Accepted answer

This script is extra useful because the door will open in the direction it is facing. That means you don't have to configure the Vector3's every time you make a new door. You simply have to rotate it the right way:

local p = script.Parent
local d = false
p.Touched:connect(function()
	if d then
		return
	end
	d = true
	for i=1, 100 do  -- 100 = Distance
		p.CFrame = p.CFrame + p.CFrame.lookVector * .1 -- .1 = Speed
		wait()
	end
	wait(1)
	for i=1, 100 do  -- 100 = Distance
		p.CFrame = p.CFrame + p.CFrame.lookVector * -.1  -- Negative here to go back
		wait()
	end
	d = false
end)

4 comments

0

This works, but instead of going to the left into the wall, if comes forward towards myself, I have tried rotating but to no prevail. Any ideas?

0

It gets confusing rotating the part, the resizing everything (I've been through it too). The best thing is to completely shrink the door to size 1,1,1, and then rotate it until it's right. Then expand it back to normal.

0

Thanks, I got it right as soon as you posted that comment. Thank you so much for your help. :)

0

No problem! I would highly appreciate an upvote on my answer and a click of the "Accept Answer" button c:

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