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

How can I make my door move slowly to a certain position instead of disappearing?

I am creating a sliding door with a model right now, and it's supposed to start on the click of a button, however, when I clicked the button the door panel just disappears and the button won't reset. It should move a little bit and then wait 3 seconds and move back. Here's the code.

game.Workspace.Button.ClickDetector.MouseClick:connect(function()
local Side2 = script.Parent
local MainDoor = script.Parent.Door2
game.Workspace.Button.ClickDetector.MaxActivationDistance = 0
game.Workspace.ButtonLight.BrickColor = BrickColor.new("Lime green")

Side2.PrimaryPart = MainDoor
for i = 0,1,0.001 do
	Side2:SetPrimaryPartCFrame(Side2:GetPrimaryPartCFrame() + Vector3.new(MainDoor.CFrame.X + i, MainDoor.CFrame.Y, MainDoor.CFrame.Z))
	wait(0.2)
end
wait(3)
for i = 0,1,0.001 do
	Side2:SetPrimaryPartCFrame(Side2:GetPrimaryPartCFrame() + Vector3.new(MainDoor.CFrame.X - i, MainDoor.CFrame.Y, MainDoor.CFrame.Z))
	wait(0.2)
end
game.Workspace.ButtonLight.BrickColor = BrickColor.new("White")
game.Workspace.Button.ClickDetector.MaxActivationDistance = 30
end)


2 comments

0

gg

0

use Lerp()

2 answers

There are few problems with your script. First of all when you are adding Vector3 to the PrimaryPartCFrame, you should only add one little step (and not the whole Position), otherwise your panel will go away very very far.

Side2:SetPrimaryPartCFrame(Side2:GetPrimaryPartCFrame() + Vector3.new(0.001,0,0))
Side2:SetPrimaryPartCFrame(Side2:GetPrimaryPartCFrame() - Vector3.new(0.001,0,0))

Secondly you have wayyyy tooo many steps. Your opening/closing animation will take 0.2 * 1000 ~ 200 seconds each. Reduce the step to 0.1 from 0.001 it won't make much difference in smoothness.

Apart from that you should really use TweenService, or at least lerp for that kind of animations. They are optimized for Roblox and are much less resource heavy than changing CFrame every step. Let me know if you'd like me to rewrite your script to use the TweenService.

Using TweenService

This class is a Service! It is a top-level singleton which can be retrieved using the GetService function.

This object cannot be created with the Instance.new constructor function.


Tweens are used to interpolate the properties of instances. These can be used to create animations for various Roblox objects. Almost any numeric property can be tweened using TweenService. Note that only specific types of properties can be used with TweenService. The types of properties that can be tweened are:

  • number
  • bool
  • CFrame
  • Rect
  • Color3
  • UDim
  • UDim2
  • Vector2
  • Vector2int16
  • Vector3

TweenService has just one function, TweenService:Create, which takes information about the animation and generates the Tween object which can be used to play the animation. Note that Tweens can animate multiple properties at the same time.

Details on how the interpolation of the tween is to be carried out are given in the tweenInfo parameter of TweenService:Create. The TweenInfo data type includes a range of properties that can be used to achieve various styles of animation, including reversing and looping Tweens (see examples).

Multiple tweens can be played on the same object at the same time, but they must not be animating the same property. If two tweens attempt to modify the same property, the initial tween will be cancelled and overwritten by the most recent tween (see examples).

Although other methods exist for tweening objects, such as GuiObject:TweenSizeAndPosition, TweenService allows multiple properties to be modified and for the animation to be paused and cancelled at any point.


How to apply TweenService to my code?

local TweenService = game:GetService("TweenService") -- Get tweenservice
local debounce = false -- Debounces are pretty much like cooldown in programming

local tweenInfo = TweenInfo.new(
1, -- How long tweening will take
Enum.EasingStyle.Sine, -- Know more about EasingStyles here: https://developer.roblox.com/api-reference/enum/EasingStyle
Enum.EasingDirection.InOut, -- Know more about EasingDirections here: https://developer.roblox.com/api-reference/enum/EasingDirection
0, -- How many times it will repeat
true, -- Choose either to make the tween go reverse after tweening normally. (In this case you wanna put true since you want to make the door close after 3 seconds
3, -- Delay before tweening again (Reverse)
)

game.Workspace.Button.ClickDetector.MouseClick:Connect(function() -- Do not use :connect, it is deprecated. Use :Connect instead
	if debounce == false then -- If debounce is false
		debounce = true -- Make debounce into true
		local Side2 = script.Parent
		game.Workspace.ButtonLight.BrickColor = BrickColor.new("Lime green")
		for _, v in pairs(Side2:GetDescendants()) do -- Get all descendants of Side2
			if v:IsA("BasePart") then -- Check if descendant is a BasePart
				TweenService:Create(v, tweenInfo, {CFrame = v.CFrame + Vector3.new(5, 0 ,0)}):Play() -- Creates a tween and plays it; Move 5 studs at the X axis. Edit if you want
			end
		end
		wait(5)
		debounce = false
	end
end)

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