Ad
Log in to vote
0

How to make door that opens on button click then closes on a second button click using Tweens?

Asked by 13 days ago

I'm trying to make a door that opens when you click a button. Then I want it to close after you click the same button. It opens but does not close. The print message "closing" that I have printed so it's a problem with the tween2 I believe. Any ideas?

local TweenService = game:GetService("TweenService")

local button = game.Workspace.Button.ClickDetector
local door = script.Parent

local tweenInfo = TweenInfo.new(3,Enum.EasingStyle.Cubic,Enum.EasingDirection.InOut,0,false)

local opened = {}
opened.Position = Vector3.new(-36.9, 2.9, -19.675)

local yolk = {}
door.Position = Vector3.new(-31.75, 2.9, -19.675)

local tween1 = TweenService:Create(door,tweenInfo,opened)
local tween2 = TweenService:Create(door,tweenInfo,yolk)

local function yeetSwag()
    if door.Position == Vector3.new(-36.9, 2.9, -19.675) then
        print("closing")
        tween2:Play()
    else
        print("opening...")
        tween1:Play()
    end
end

button.MouseClick:Connect(yeetSwag)

1 answer

Log in to vote
0
Answered by
imKirda 2268 Moderation Voter Community Moderator
13 days ago

you had a typo on line 12 or you forgot to add property to the yolk, take a look

local yolk = {}
door.Position = Vector3.new(-31.75, 2.9, -19.675)
-- door.Position?

Didn't you mean

local yolk = {}
yolk.Position = Vector3.new(-31.75, 2.9, -19.675)
-- yolk.Position

Because in your case yolk is empty so it is not supposed to tween anything. You also should implement simple debounce rather than checking if door's position is Vector3... because if it's not it will play tween1 again which is not the best in my opinion. Debounce in your case would be a value if the door is closing/closed or if it is opening/open.

0
The yolk was just a variable for the door's closed position. MathinatorMatthew -14 — 13d
0
The yolk actually worked. I forgot I was supposed to basically define yolk's properties. MathinatorMatthew -14 — 13d
Ad

Answer this question