TweenService on CFrame

I made a script which lets the player open and close a door with a mouse click. I used CFrame because I want the door to move 6 studs up when opened and 6 studs down when closed. My issue is that the door just instantly teleports there and I need to use TweeningService to change that but I’m very confused and I cant implement it on my script.
I searched on the devforum for people with the same issue that want to use tweening on CFrame but it wasn’t the same situation as me.

I already made the TweenInfo but I don’t really know what to do after that.
Here is the code :

local door = script.Parent

local ts = game:GetService("TweenService")
local tsInfo = TweenInfo.new(1.5, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut)

local opn = false
local cls = true

local cd = workspace.BillisButton.ClickDetector

local function doorOpen()
	door.CFrame = door.CFrame * CFrame.new(0, 6, 0)
end

local function doorClose()
	door.CFrame = door.CFrame * CFrame.new(0, -6, 0)
end

cd.MouseClick:Connect(function()
	if cls == true then
		cls = false
		opn = true
		doorOpen()
	elseif opn == true then
		cls = true
		opn = false
		doorClose()
	end
end)

To create a tween using tweenservice, you use:

TweenService:Create(INSTANCE, TWEENINFO, PROPERTIES)

The tween then needs to be played

TWEEN:Play()

I added this to your script. It should have the desired outcome!

local door = script.Parent

local ts = game:GetService("TweenService")
local tsInfo = TweenInfo.new(1.5, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut)

local open = false

local cd = workspace.BillisButton.ClickDetector

local function doorOpen()
	endCFrame = door.CFrame + Vector3.new(0, 6, 0)
    ts:Create(door, tsInfo, {CFrame = endCFrame}):Play()
end

local function doorClose()
	endCFrame  = door.CFrame + Vector3.new(0, -6, 0)
    ts:Create(door, tsInfo, {CFrame = endCFrame}):Play()
end

cd.MouseClick:Connect(function()
    open = not open
    if open then
	    doorOpen()
    else
		doorClose()
	end
end)

I also modified the last part of your code to be simpler

1 Like

It worked, thanks! But can you tell me exactly how the ts:Create(this part specifically) works?

The properties part kinda confused me

Basically, the third parameter is a table detailing what the outcome should be.

The syntax for the table is:

{
    PROPERTY = VALUE,
}

You can have multiple outcomes at once, such as:

{
    PROPERTY1 = VALUE1,
    PROPERTY2 = VALUE2,
}

For example, if you want to change the position of an object to Vector3.new(0, 10,0) (ten studs above the origin), then you would put the third parameter as

{
    Position = Vector3.new(0, 10, 0)
}
1 Like

Ohhh alright thanks. Also one last thing, do you know how should I add a cooldown so I can’t spam the door? Don’t hand me the script, tell me instead so Ill find out my self. Sorry for bothering you this much.

You would create a variable, say DoorMoving, which you would initially set to false.
Then, as the first line within the MouseClick function, you would check if the DoorMoving variable is true, and if it IS true then you would return. If not, have the code proceed as usual.

Just after the checking if it is true, you would add DoorMoving = true. Then after all the code within the function, add a wait() for 1.5 seconds, then after the 1.5 seconds set the value of DoorMoving to false again.

1 Like

Thank you so much! I really appreciate your help :+1:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.