Issue with my tweening script

It works well, however I want it to go back after it completes the tween, right now it just snaps back to the start of the tween instead of going backwards

local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

local localPlayer = Players.LocalPlayer
local camera = workspace.CurrentCamera
local partB = workspace.Cam.B

local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("Head")

local speed = 1
local distance = (partB.Position - humanoidRootPart.Position).Magnitude
local duration = distance / speed
local cameraTweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Linear)
local targetCFrame = humanoidRootPart.CFrame * CFrame.new(0, 0, -9 * distance)

local tween = TweenService:Create(camera, cameraTweenInfo, {CFrame = targetCFrame})
tweenBackWards = TweenService:Create(camera, cameraTweenInfo, {CFrame = targetCFrame})
local playerGui = script.Parent
local greenButton = playerGui:WaitForChild("Green"):WaitForChild("Start")

local isMovingForward = true 

greenButton.Activated:Connect(function()
	tween:Destroy()
	tweenBackWards:Destroy()
	script.Parent.Green:Destroy()
	script.Parent.Background.Frame.Visible = true
	script.Parent.Background.Frame.BackgroundTransparency = 0 
		wait(1)
	script.Parent.Background.Frame.BackgroundTransparency += 0.1
	wait(.5)
	script.Parent.Background.Frame.BackgroundTransparency += 0.1
	wait(.5)
	script.Parent.Background.Frame.BackgroundTransparency += 0.1
	wait(.5)
	script.Parent.Background.Frame.BackgroundTransparency += 0.1
	wait(.5)
	script.Parent.Background.Frame.BackgroundTransparency += 0.1
	wait(.5)
	script.Parent.Background.Frame.BackgroundTransparency += 0.1
	wait(.5)
	script.Parent.Background.Frame.BackgroundTransparency += 0.1
	wait(.5)
	script.Parent.Background.Frame.BackgroundTransparency += 0.1
	wait(.5)
	script.Parent.Background.Frame.BackgroundTransparency += 0.1
	greenButton:Destroy()
	tween:Destroy()
	tweenBackWards:Destroy()
end)
while true do


	tween:Play()
	tween.Completed:Wait()
	tween:Cancel()
	tweenBackWards:Play()
	tweenBackWards.Completed:Wait()
end
1 Like

Set the reverses parameter in your TweenInfo to true.

new(time: number, easingStyle: EasingStyle, easingDirection: EasingDirection, repeatCount: number, reverses: boolean, delayTime: number)

1 Like

I dont know anything about tweening but the part that bothers me is the one where is increases transparency you can make it shorter by using a β€˜for’ function

3 Likes

Yeah I know I was using it as an example to someone and forgot to change it, but why would you use a for loop?

A better way to do this would probably be to use Tweens, but this is how it would look with a for loop:

for i = 0, 1, 0.1 do
    script.Parent.Background.Frame.BackgroundTransparency = i
    wait(0.5)
end
1 Like

Hmm thats very useful thank you!

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