Tween behaving strangely

Hello, recently I have been working on a capsule that opens using tweenservice. when activated, It behaves normally the first time. However, each time after that the opening tween acts strangely. Below I will provide the script, as well as a game with the capsule for demonstration and visual aid. Thanks for reading.

OpenPos = script.Parent.Orientation.Y
IsInUse = false
TweenService = game:GetService("TweenService")

TweeningInfo = TweenInfo.new(
	2,
	Enum.EasingStyle.Sine,
	Enum.EasingDirection.In,
	0,
	false,
	0
)
OpenGoal = {}
OpenGoal.Orientation = Vector3.new(0,OpenPos+180,0)
CloseGoal = {}
CloseGoal.Orientation = Vector3.new(0,OpenPos,0)

OpenTween = TweenService:Create(script.Parent, TweeningInfo, OpenGoal)
CloseTween = TweenService:Create(script.Parent, TweeningInfo, CloseGoal)

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	if IsInUse == false then
		IsInUse = true
		script.Parent.ClickDetector.MaxActivationDistance = 0
		wait(0.2)
		OpenTween:Play()
		wait(5)
		if IsInUse == true then
			CloseTween:Play()
			script.Parent.ClickDetector.MaxActivationDistance = 16
			IsInUse = false
		end
	end
end)

Why are you checking IfInUse twice? You only need to check it once, as you don’t set it to false in the first place before checking.

Here’s what you can do:

if IsInUse then return end
IsInUse = true
-- Code.
IsInUse = false

Also, you should use local variables as there are no need for global ones in your code.

1 Like

Ok, I updated the script but it still does the thing with the tween

local OpenPos = script.Parent.Orientation.Y
local TweenService = game:GetService("TweenService")

local TweeningInfo = TweenInfo.new(
2,
Enum.EasingStyle.Sine,
Enum.EasingDirection.In,
0,
false,
0
)

OpenGoal = {}
OpenGoal.Orientation = Vector3.new(0,OpenPos+180,0)
CloseGoal = {}
CloseGoal.Orientation = Vector3.new(0,OpenPos,0)

OpenTween = TweenService:Create(script.Parent, TweeningInfo, OpenGoal)
CloseTween = TweenService:Create(script.Parent, TweeningInfo, CloseGoal)

script.Parent.ClickDetector.MouseClick:Connect(function()
    script.Parent.ClickDetector.MaxActivationDistance = 0
    OpenTween:Play()
end)

script.Parent.Parent.TouchPart.Touched:Connect(function()
    CloseTween:Play()
    script.Parent.ClickDetector.MaxActivationDistance = 16
end)

It seems like it’s doing an extra u-turn, that is caused by the extra orientation you added (most likely).
You should double-check its orientation when it’s open and closed, then grab their current orientation.