How can I make a part tween in the direction it's rotated?

I currently have a problem with a toggleable switch where I will tween it and it will work but if I rotate it and then tween it, the tween goes in the same exact position.

This is the current code:

-- variables
local part = script.Parent
local clickDetector = part.Parent.ClickDetector
local tweenService = game:GetService("TweenService")
local sound = part.Parent.Sound

-- positions
local switchpos = part.Position
local target1 = part.Orientation + Vector3.new(35, 0, 0)
local target2 = part.Orientation + Vector3.new(-35, 0, 0)

-- nil variables
local pos = 0
local debounce = false

-- tweeninfo
local tweenInfo = TweenInfo.new(
	0.1,
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

-- tweening
local goal1 = {}
goal1.Orientation = target1

local goal2 = {}
goal1.Orientation = target2

local tween = tweenService:Create(part, tweenInfo, goal2)
local rtween = tweenService:Create(part, tweenInfo, goal1)


-- click/tween
clickDetector.MouseClick:Connect(function()
	
	-- tween edit
	switchpos = part.Position
	target1 = part.Orientation + Vector3.new(70, 0, 0)
	target2 = part.Orientation + Vector3.new(-70, 0, 0)
	goal1.Orientation = target1
	goal2.Orientation = target2
	
	tween = tweenService:Create(part, tweenInfo, goal1)
	rtween = tweenService:Create(part, tweenInfo, goal2)	
	
	-- pos 0
	if pos == 0 and debounce == false then
		debounce = true
		sound:Play()
		tween:Play()
		pos = 1
		wait(0.35)
		debounce = false
		
	-- pos 1
	elseif pos == 1 and debounce == false then
		debounce = true
		sound:Play()
		rtween:Play()
		pos = 0
		wait(0.35)
		debounce = false
		
	end
end)

I think you should use CFrame instead:

-- variables
local part = script.Parent
local clickDetector = part.Parent.ClickDetector
local tweenService = game:GetService("TweenService")
local sound = part.Parent.Sound

-- nil variables
local pos = 0
local debounce = false

-- tweeninfo
local tweenInfo = TweenInfo.new(
    0.1,
    Enum.EasingStyle.Quad,
    Enum.EasingDirection.Out,
    0,
    false,
    0
)

-- click/tween
clickDetector.MouseClick:Connect(function()
    if debounce then return end
    debounce = true

    local targetCFrame1 = part.CFrame * CFrame.Angles(math.rad(70), 0, 0)
    local targetCFrame2 = part.CFrame * CFrame.Angles(math.rad(-70), 0, 0)

    local tween1 = tweenService:Create(part, tweenInfo, {CFrame = targetCFrame1})
    local tween2 = tweenService:Create(part, tweenInfo, {CFrame = targetCFrame2})

    sound:Play()

    if pos == 0 then
        tween1:Play()
        pos = 1
    else
        tween2:Play()
        pos = 0
    end

    wait(0.35)
    debounce = false
end)

I changed from using tweens to using a motor6d for the switch. Fixed it and now I can move and rotate it without it breaking.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.