I need help with tweening this door

You can write your topic however you want, but you need to answer these questions:
I want to make a slider door that’s going to be activated via clickdetector.
The issue is that my tween won’t work when I click on it and it just says: Unable to cast to Dictionary.

local TweenService = game:GetService("TweenService")
local Door = script.Parent.GlassIn
local DoorInfo = "Closed"
local ClickDetector = script.Parent.Handle.ClickDetector
local Debounce = false
local DoorClosedPosition = Door.Position
local DoorOpenPosition = {-199.356, 5.735, 397.158}

local TweenInfo = TweenInfo.new(
	1,
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.InOut,
	0,
	false,
	0
)

ClickDetector.MouseClick:Connect(function(open)
	if Debounce == true then return end
	Debounce = true
	if DoorInfo == "Closed" then
		TweenService:Create(DoorClosedPosition, TweenInfo, DoorClosedPosition + Vector3.new(2.28,0,0))  -- This is where the error is
		DoorInfo = "Open"
	else
		TweenService:Create(DoorClosedPosition + Vector3.new(2.28,0,0), TweenInfo, DoorClosedPosition)
		DoorInfo = "Closed"
	end
	    wait(1.5)
	    Debounce = false
end)

Any help?

2 Likes

try this

ClickDetector.MouseClick:Connect(function(open)
	if Debounce == true then return end
	Debounce = true
	if DoorInfo == "Closed" then
		TweenService:Create(Door, TweenInfo, {Position=DoorClosedPosition + Vector3.new(2.28,0,0)})  :Play()
		DoorInfo = "Open"
	else
		TweenService:Create(Door, TweenInfo, {Position = DoorClosedPosition}):Play()
		DoorInfo = "Closed"
	end
	 wait(1.5)
	 Debounce = false
end)

sorry i forgot to put :Play()

1 Like

yeah this works, you tried inserting the goal of the tween without actually using {}.

What you wrote:

TweenService:Create(DoorClosedPosition, TweenInfo, DoorClosedPosition + Vector3.new(2.28,0,0))

What Nube762 tweaked:

TweenService:Create(Door, TweenInfo, {Position=DoorClosedPosition + Vector3.new(2.28,0,0)}) :Play()

Basically, he enclosed the tween goal into a set of *braces (a dictionary) and added the :Play() which will play the tween created in the specified TweenService:Create() to the end.

It’s a common issue everyone has at first with tweens, just remember to always enclose the Tween Goal within braces.

Also - Tweak your Door Closed Position to be the position of the door model while it is actually closed instead of just as “Door.Position” - Don’t set it to just the door position as that will mean it’ll never close and the tween will visibly do nothing.

EDIT: corrected brackets to braces and expanded some parts for context

1 Like

Here’s the function I use for tweening parts:

local function TweenPart(Part, ToGo, Time)
    local ts = game:GetService("TweenService")
    local ti = TweenInfo.new(Time, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0)
    local tp = {CFrame = ToGo}

    local t = ts:Create(Part, ti, tp)
    t:Play()
end

You then need to place the door part, duplicate it and move another part to the open CFrame, you can then make variables of the positions and do something along the lines of, assuming (A is the visible door part, B is the open position, C is the closed position)

local debounce = false

local A = [the door instance]
local B = [the position, as a CFrame value]
local C = [closed position, as a CFrame value]
local function Cycle()
    if debounce then return end
    debounce = true
    TweenPart(A, B, 2) -- Takes 2 seconds
    wait(4) -- 2 second delay after opening
    TweenPart(A, C, 2) -- Takes 2 seconds
    wait(2)
    debounce = false
end

after that, when you have a hook to a .Touched:Connect() etc, you can have it trigger the Cycle() function, and the door opens and closes with cooldown.

If you’d like me to explain anything, let me know!

1 Like

When creating a TweenService animation, the first argument has to be the instance on which the animation is casted. The goal also has to be between {} (a table, you can leave it as just 1 argument if you want.), and tell the script what property you’re changing about the Instance.

TweenService:Create(Door, TweenInfo,{Position = DoorClosedPosition + Vector3.new(2.28,0,0)})

Make sure to reply back if it didn’t work and I’ll try it in studio. This was written on the forum.