Tweening a part on button click

Hello,

I am trying to make a barrier that opens when you click a button, and closes when you click a separate button. I have tried to search for tutorials, but the most I can find is a part that tweens constantly.

The documentation on tweening is confusing me, and I don’t really understand if I need to add remote events or something.

If someone could link a tutorial that would be great. Thank you!

4 Likes

Hello. Are you trying to tween a model or a part?

2 Likes

I am trying to tween a part, but it is inside a model if that is relevant.

1 Like

In the model, I have the two buttons and the part. Inside the buttons I have clickDetectors, and I have a localscript inside the part I am trying to tween. This is the script (which doesn’t work):

local TweenService = game:GetService("TweenService")

local Barrier = script.Parent

local openButton = script.Parent.Parent.Buttons.openButton

local closeButton = script.Parent.Parent.Buttons.closeButton

local info = TweenInfo.new(
	2,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.InOut,
	0,
	false
)

local tweenOpen = TweenService:Create(Barrier, info, {Position = -23.438, 9.25, 40.614})

local tweenClose = TweenService:Create(Barrier, info, {Position = -17.563, 3.375, 40.614})

openButton.ClickDetector.MouseClick:Connect(function(playerWhoClicked: Player) 
	tweenOpen:Play()
end)

closeButton.ClickDetector.MouseClick:Connect(function(playerWhoClicked: Player) 
	tweenClose:Play()
end)

LocalScripts don’t work in workspace. Here’s a list of where they do work:

1 Like

Oh, okay. That is useful, thank you.

If I rewrite my my code into a regular script, will that work?

Yeah it should work on a regular script.

You actually don’t have to rewrite it you can just paste that into a normal script and it will work

I did that and deleted my localScript, but it doesn’t work. I would look at output in studio test, but for some reason studio test is acting up and isn’t working. Should I replace ‘function(playerwhoclicked)’ with just ‘(function(player)’?

For this scenario, the player parameter actually isn’t required since you aren’t using the player for anything. You can delete it if it won’t be used.

Like this?

local TweenService = game:GetService("TweenService")

local Barrier = script.Parent

local openButton = script.Parent.Parent.Buttons.openButton

local closeButton = script.Parent.Parent.Buttons.closeButton

local info = TweenInfo.new(
	2,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.InOut,
	0,
	false
)

local tweenOpen = TweenService:Create(Barrier, info, {Position = -23.438, 9.25, 40.614})

local tweenClose = TweenService:Create(Barrier, info, {Position = -17.563, 3.375, 40.614})

openButton.ClickDetector.MouseClick:Connect(function()
	tweenOpen:Play()
end)

closeButton.ClickDetector.MouseClick:Connect(function()
	tweenClose:Play()
end)

Yeah that should work perfectly.

Nope, it doesn’t work. I press the button and nothing happens.

If you found a way to fix the output, add a print statement after the event to see if it even went through. If not, try checking if something is blocking your click detector, maybe the part is too small, or the clickrange is too short. Those are the only ideas that come to mind at the moment.

I will try that.

I followed this tutorial for the tweening: https://www.youtube.com/watch?v=SySp1wTsCkY&t=294s, and I assume I don’t need the ‘newPosition’ bit because I have the exact position typed out.

I have not been using studio test because it just highlights models like in regular studio, so you can’t click on the buttons.

1 Like

Just deselect this:
image

After:
image

1 Like

It looks like it doesn’t go through. The only output relating to the script is:

21:50:14.740 Unable to cast to Dictionary - Server - Script:17
21:50:14.740 Stack Begin - Studio
21:50:14.740 Script ‘Workspace.barrierIn_left.Part.Script’, Line 17 - Studio - Script:17
21:50:14.740 Stack End - Studio

Line 17 in question is:

local tweenOpen = TweenService:Create(Barrier, info, {Position = -23.438, 9.25, 40.614})

I must be the stupidest person on Earth. Thanks

1 Like

Ohhh! I see what the problem was. For the position value, put it inside a Vector3.

Position = Vector3.new(your position here)

1 Like

Yep, this works. Thank you for all your help.

1 Like