Hello, Today I will show you how to make a door with TweenService.
So, for this tutorial I am going to use Vector3 Tween.
First of all, We need to make a simple door.
I've made an example here:
After you made a door, Make sure all the parts of the door are anchored.
Like on the image:
Also, Don't forget to rename the parts and make sure the CanCollide in set ON.
After you've done that let's move on the scripting part.
First we will localize TweenService and Door.
local Service = game:GetService("TweenService") --Localing TweenService
local Door = script.Parent.Parent.Door --Localing door
Now, We will create New TweenInfo.
local Info = TweenInfo.new(1.5) --number says how much seconds need to open door
After that We will make a MouseClick Function.
Before we start working on Function, Make sure the Button on your door has a ClickDetector and Script in it.
So, We will type in:
script.Parent.ClickDetector.MouseClick:Connect(function()--when mouse clicks the button
script.Parent.CanCollide = false --disables cancollide so you can pass through doors
local Change = {Position = Vector3.new(--your position here)} --tells door to move to that position
So, now I will help you with Vector3.
First Go to Home and then click on Move Tool.
Now you will select your door and move it to end.
Now when you moved your door to the end Go to Properties and copy the doors Position.
After you copied the Position click the Undo Button.
Then Paste your Position in the script here:
Now when you pasted it we are going to Create the Service and Play the Tween.
Type in:
local Tween = Service:Create(Door, Info, Change)--creates TweenService
Tween:Play()--plays tween
end)
And We are done with the Open Button Script.
Now The Close Button Script is ALMOST similar to the open script.
Make sure that your Close Button also has a ClickDetector and Script in it.
On the Close script just change the CanCollide and the position on the door.
So, now make sure that your door is closed like on the start example.
Again click on the door and Copy It's Position.
Change the Vector3 Position and put CanCollide = true in the script.
And you're...
I will make a whole open and close script down:
local Service = game:GetService("TweenService")--localing tweenservice
local Door = script.Parent.Parent.Door --localing door
local Info = TweenInfo.new(2)--number says how much seconds need to open the door
script.Parent.ClickDetector.MouseClick:Connect(function()--when mouse clicks the button
script.Parent.CanCollide = false --disables cancollide so you can pass through doors
local Change = {Position = Vector3.new(--your position here)} --tells door to go to that position
local Tween = Service:Create(Door, Info, Change)--creates tweenservice
Tween:Play()--plays tween
end)
local Service = game:GetService("TweenService")--localing tweenservice
local Door = script.Parent.Parent.Door --localing door
local Info = TweenInfo.new(2)--number says how much seconds need to close door
script.Parent.ClickDetector.MouseClick:Connect(function()--when mouse clicks the button
script.Parent.CanCollide = true --enables cancollide so you can't pass through doors
local Change = {Position = Vector3.new(--your position here)} --tells door to go to that position
local Tween = Service:Create(Door, Info, Change)--creates tweenservice
Tween:Play()--plays tween
end)
That's It for this Tutorial.
Thanks for viewing this tutorial.