Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to Tween a part using a script?

Hello. I am relativley new to Tweening, and I wanna tween a part. I know how to Tween a UI but not parts. I Have this so far:

local click = script.Parent.Click
local closed = true
local door = script.Parent.Parent.Parent.Parent.Door
local tService = game:GetService("TweenService")
click.MouseClick:Connect(function()
	if closed == true then
		script.Parent.BrickColor = BrickColor.new("Bright green")
		door.Position = UDim2.new(83, 5, 56.5)
		closed = false
	  else
		if closed === false then
--add lines later
		end
	end
end)

1 answer

*Correction: Part objects use Vector3's for Size, Position, and Orientation; while UI objects use UDim2's.

To properly tween BaseParts (any 3d geometry like Unions, Parts, and MeshParts), use the TweenService:Create function.

=============================== How to ===============================

You need any tweenable object, a TweenInfo, and a table with a list of property names and their goal values.

For example:

Making a simple TweenInfo

local Simple_TweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)

Making a simple table with property names

local Simple_Table = {
	Position = Vector3.new(2, 10, 2),
	Size = Vector3.new(5, 5, 5)
}

Making a Tween

local Tween = TweenService.new(TargetPart, Simple_TweenInfo, Simple_Table)

Playing a Tween

Tween:Play()

Pausing/Stopping a Tween

Tween:Pause() -- Stops Tween, doesn't reset.
Tween:Cancel() -- Stops Tween, resets.

=============================== Links ===============================

For more information about Tweens, TweenService, and TweenInfo, here are some links: TweenService: https://developer.roblox.com/en-us/api-reference/class/TweenService Tween: https://developer.roblox.com/en-us/api-reference/class/Tween TweenInfo: https://developer.roblox.com/en-us/api-reference/datatype/TweenInfo

=============================== Final ===============================

So, to finish the answer, here is how you can use it:

local click = script.Parent.Click
local closed = true
local door = script.Parent.Parent.Parent.Parent.Door

local tService = game:GetService("TweenService")
local tInfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out) --
local tGoal = {Position = Vector3.new(83, 5, 56.5)} --

local tween = tService.new(door, tInfo, tGoal) --

click.MouseClick:Connect(function()
    if closed == true then
        script.Parent.BrickColor = BrickColor.new("Bright green")
        tween:Play() --
        closed = false
      else
        if closed === false then
--add lines later
        end
    end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.