Need help with tween script

  1. I want my Inventory frame to tween closed after tweening open.

  2. The Inventory frame only tweens open, and doesn’t work after.

LocalScript:

local TweenService = game:GetService("TweenService")

local Information = {
	Inventory = TweenInfo.new(0.3,Enum.EasingStyle.Sine,Enum.EasingDirection.Out);
	Settings = TweenInfo.new(0.3,Enum.EasingStyle.Sine,Enum.EasingDirection.Out);
}

local Goals = {
	InventoryOpen = {
		Position = UDim2.new(0.243, 0,0.178, 0);
	};
	InventoryClose = {
		Position = UDim2.new(0.243, 0,-0.75, 0);
	};
	SettingsOpen = {
		Position = UDim2.new(0.225, 0,0.499, 0);
	};
	SettingsClose = {
		Position = UDim2.new(0.225, 0,-0.3, 0);
	};
}

local Tweens = {
	InventoryOpen = TweenService:Create(script.Parent.Parent.Inventory2,Information.Inventory,Goals.InventoryOpen);
	InventoryClose = TweenService:Create(script.Parent.Parent.Inventory2,Information.Inventory,Goals.InventoryClose);
	SettingsOpen = TweenService:Create(script.Parent.Parent.Settings,Information.Settings,Goals.SettingsOpen);
	SettingsClose = TweenService:Create(script.Parent.Parent.Settings,Information.Settings,Goals.SettingsClose);
}

script.Parent.Inventory.Button.MouseButton1Down:Connect(function()
	if script.Parent.Parent.Inventory2.Position == Goals.InventoryClose.Position then
		Tweens.InventoryOpen:Play()
	end
	if script.Parent.Parent.Inventory2.Position == Goals.InventoryOpen.Position then
		Tweens.InventoryClose:Play()
	end	
end)

script.Parent.Settings.Button.MouseButton1Down:Connect(function()
	if script.Parent.Parent.Settings.Position == Goals.SettingsClose.Position then
		Tweens.SettingsOpen:Play()
	end
	if script.Parent.Parent.Settings.Position == Goals.SettingsOpen.Position then
		Tweens.SettingsClose:Play()
	end	
end)

If anyone has a solution, then please let me know.

1 Like

You don’t need the tweenservice for UI’s, use the built in TweenPosition() function.

2 Likes

There are a lot of things you should change here. Firstly, you should be using TweenPosition() as I said above. Secondly, you should have a variable set to a bool for each menu. for example:

local SettingsOpen = false
local InventoryOpen = false

You can then use that in place of:

if script.Parent.Parent.Settings.Position == Goals.SettingsClose.Position then

It can instead be:

if InventoryOpen then
	--Your Close Tween
	inventoryOpen = false
else
	--Your Open Tween
	inventoryOpen = true
end

I would also recommend splitting your functions and your events. For your case it’s not that important, but in the future if you want to have the Inventory close when a key is pressed, or what someone presses a different button, rather than being redundant and typing out all of the things again, you can split it up into multiple functions, like this:

local function openInventory()
	--Your Open Tween
	--Changing the Visible variable of the frame
	inventoryOpen = true
end

script.Parent.Inventory.Button.MouseButton1Down:Connect(function()
	if InventoryOpen then
		openInventory()
	else
		closeInventory()
	end
end)

Alternatively you could have a separate function just to handle button inputs in a function.

local function inventoryButtonPressed()
	--handle if statements and tweens here
end

script.Parent.Inventory.Button.MouseButton1Down:Connect(inventoryButtonPressed)
1 Like