TweenService runs twice and stops working

Hello!
I’m trying to make a door system using TweenService. It’s supposed to close when a player touches a button and to open when the player touches again the button.

It closes and opens well, but it stops working when I try to close it for second time.

I tried to do loops and returns, but nothing worked.

local MPS = game:GetService("MarketplaceService")
local passId = 9917540
local TweenService = game:GetService("TweenService")
local ClickMe = game.Workspace.ClickMeGamePass.ClickDetector
local Door = game.Workspace.SecurityDoor
local openPosition = game.Workspace.OpenedDoor
local closedDooor = game.Workspace.ClosedDoor

function onMouseClick()
    local Info = TweenInfo.new(
	    2,
	    Enum.EasingStyle.Linear,
	    Enum.EasingDirection.Out,
	    0, 
	    false,
	    0
)

local Goals = {
    Position = Vector3.new(-1.44, 4.5, -1.485)
}

local OpenGoals = {
    Position = Vector3.new(-1.44, 13.5, -1.485)
}

if Door.Position == openPosition.Position then
	local closeDoorTween = TweenService:Create(Door, Info, Goals)
    closeDoorTween:Play()

	else
	
	local openDoorTween = TweenService:Create(Door, Info, OpenGoals)
    openDoorTween:Play()
   end
end

game.Players.PlayerAdded:Connect(function(Player)
    if MPS:UserOwnsGamePassAsync(Player.UserId, passId) then
        end
end)

ClickMe.MouseClick:Connect(onMouseClick)

I’m not sure if this is the exact issue but Roblox engine likes adding weird offsets to the positions. So, if the door is even 0.000001 stud away from the position, then it will just keep trying to open it because it think that it’s closed. I recommend that you use a BoolValue instead to check if the door is opened.

1 Like