Tweening camera isn't working

Hello, i want to have an animation when i press q and the camera goes to a position on the other side of the character.

My Code:

local camera = workspace.CurrentCamera
camera.CameraType = "Scriptable"
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
UserInputService.InputChanged:Connect(function(inputObject, busy)
    if busy then
        return
    end
    
    if inputObject.UserInputType.Name == "MouseMovement" then
        local delta = inputObject.Delta
        xAng = math.clamp(xAng - delta.y*sensitivity*math.pi/180, minAngle*math.pi/180, maxAngle*math.pi/180)
        yAng = (yAng - delta.x*sensitivity*math.pi/180)%(2*math.pi)
    end
end)

RunService.RenderStepped:Connect(function()
    local character = PlayerService.LocalPlayer.Character
    if not character then
        return
    end
    
    local rootPart = character:FindFirstChild("HumanoidRootPart")
    if not rootPart then
        return
    end
    
    if lockMouse then UserInputService.MouseBehavior = "LockCenter" else end
    local cameraCFrame = CFrame.new(0, 1.5, 0)*CFrame.Angles(0, yAng, 0)*CFrame.Angles(xAng, 0, 0)*CFrame.new(offset)+ rootPart.CFrame.p
	camera.CFrame = cameraCFrame
	
	UserInputService.InputEnded:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.Q then
			offset = Vector3.new(-2,2,7)
			tweenService:Create(camera, tweenInfo, {CFrame = CFrame.new(offset)}):Play()
		end
	end)
	UserInputService.InputEnded:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.E then
			offset = Vector3.new(-2,2,7)
		end
	end)
end)

video of what i have but not animated:

video of my tweening(game starts to lag after multiple attempts):

When i have my tween the game starts to lag if i press the button a lot. How can i make a smooth animation that goes to the correct offset and not lag?

Take a look at this;

Press Here

You mean so a tween should tween more slowly to specific location? If so then you just have to increase the TweenInfo’s first parameter. In your case is set to 0.5 seconds which is very fast. Also when you tween more longer tween I’d recommend you use next syntax so the script is going to wait for tween to end.

--Declaration of tween and etc.
tween:Play()
tween.Completed:Wait()
--Things to happen after tween is done

the issue is its going to the position of the current camera in the workspace. The offset for the right shoulder is 2,2,7 and the offset for the right shoulder is -2,2,7. I want it to have a clean animation when you press E or Q so the camera goes to that position. This is shown in the second video.

so when you press e the camera goes to right and whe q to left?

i will take the video as a yes

look, you set the camera CFrame to (-2,2,7) wich is the coordinates in the world and not relative to the character or HumanoidRoot part

all you need to do is this

tweenService:Create(camera, tweenInfo, {CFrame = rootPart.CFrame * CFrame.new(offset)}):Play()
  1. You have UserInputService.InputEnded:Connect() called twice, NESTED inside RunService.RenderStepped. That’s going to cause lag issues.
  2. Turns out, it’s impossible to use TweenService for moving cameras. TweenService has one goal and one goal only. The goal cannot be changed while it is being played.
  3. Since your EasingStyle is only linear, you can do this without having to use TweenService and rather CFrame:Lerp()
    I will provide you an working example as soon as possible.
    Also: Thanks for not including the other variables, could have saved me a lot of time if I didn’t have to redefine RunService and UserInputService.

Here is my version:

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local PlayerService = game.Players

--random variables i just defined so it would stop giving me errors
local sensitivity = 0.5
local minAngle = -90
local maxAngle = 90
local lockMouse = false
local yAng = 0
local xAng = 0

local rootPart = PlayerService.LocalPlayer.Character:WaitForChild("HumanoidRootPart")

local offsetLeft = Vector3.new(-2,2,7)
local offsetRight = Vector3.new(2,2,7)

--step stuff
local step = 0
local stepTarget = 1
local stepDelta = 0.1


local camera = workspace.CurrentCamera
camera.CameraType = "Scriptable"

--dont know what this does but im keeping it here
UserInputService.InputChanged:Connect(function(inputObject, busy)
	if busy then
		return
	end
	
	if inputObject.UserInputType.Name == "MouseMovement" then
		local delta = inputObject.Delta
		xAng = math.clamp(xAng - delta.y*sensitivity*math.pi/180, minAngle*math.pi/180, maxAngle*math.pi/180)
		yAng = (yAng - delta.x*sensitivity*math.pi/180)%(2*math.pi)
	end
end)

RunService.RenderStepped:Connect(function()
	local character = PlayerService.LocalPlayer.Character
	
	if step<stepTarget then
		step += stepDelta
		if step>stepTarget then
			step = stepTarget
		end
	elseif step>stepTarget then
		step -= stepDelta
		if step<stepTarget then
			step = stepTarget
		end
	end	
	
	if lockMouse then UserInputService.MouseBehavior = "LockCenter" else end
	
	local offset = offsetLeft:Lerp(offsetRight, step)
	local cameraCFrame = CFrame.new(0, 1.5, 0)*CFrame.Angles(0, yAng, 0)*CFrame.Angles(xAng, 0, 0)*CFrame.new(offset)+ rootPart.CFrame.p
	camera.CFrame = cameraCFrame
end)

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		stepTarget = 1
	elseif input.KeyCode == Enum.KeyCode.Q then
		stepTarget = 0
	end
end)

I tried this but it makes the camera move down towards the rootpart and then teleports the camera to the offset without an animation. Also another issue that i didn’t address is when you run and press E or Q the camera is scriptable so it will stay in the position until the animation is played.

That’s because after the tween has ended, the camera goes back to what it was:

local cameraCFrame = CFrame.new(0, 1.5, 0)*CFrame.Angles(0, yAng, 0)*CFrame.Angles(xAng, 0, 0)*CFrame.new(offset)+ rootPart.CFrame.p
camera.CFrame = cameraCFrame

thanks your version worked, i’ll play around with it and see what else can be done with it