Tweening problem when clicking button

Hi, I am trying to make a tweening effect where the player presses a button, and it plays that animation, but for some reason the camera goes back to its original position after the tweening animation ends. I am not sure whether it is another code doing it or If I messed something up on the tweening code. The screenshots and codes I provided, what I think, are involved in the situation (I also included a video to make it more understandable to what I am trying to achieve):

ServerScriptService:

local remote = game.ReplicatedStorage.Menu
game.Players.PlayerAdded:Connect(function(plr)
remote:FireClient(plr)
end)

Lines 16 through 28 from the Main Script of the ScreenGui:

function mainMenu()
	repeat
		cam.CameraType = Enum.CameraType.Scriptable
		wait(.1)
	until	cam.CameraType == Enum.CameraType.Scriptable
	textlabel.Visible = true
	playbutton.Visible = true
	cam.CFrame = camPart.CFrame
end

remote.OnClientEvent:Connect(function()
	mainMenu()
end)

The local script parented to the button that plays the tween:

OpenShop.MouseButton1Click:Connect(function()
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = CamPart.CFrame
	local tween = TweenService:Create(Camera, TweenInfo.new(1.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0), {CFrame = CamPart2.CFrame})
	tween:Play()
end)

The video:

If anyone can fix my code or tell me what the problem was that would be helpful because I have been struggling with solving this for a while now.

I notice you got a pretty cool camera movement that looks like it follows the mouse. Is it possible that the code is setting it back to the camPart's CFrame? If it is, you should have the camera shake effect be relative to the workspace.CurrentCamera CFrame and not another part.

1 Like

Yeah I think the part that handles the lookvector for the mouse movement is referencing a cameraPart position instead of the actual camera position.

Try setting the camera’s position after the tween completed

Which one do I change?

-- Variables
local cam = workspace.CurrentCamera
local camPart = workspace.CamPart
local mouse = game:GetService("Players").LocalPlayer:GetMouse()

-- Set Camera
repeat
	wait()
	cam.CameraType = Enum.CameraType.Scriptable
until
cam.CameraType == Enum.CameraType.Scriptable

-- Move cam
local maxTilt = 10
game:GetService("RunService").RenderStepped:Connect(function()
	cam.CFrame = camPart.CFrame * CFrame.Angles(
		math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),
		math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),
		0
	)
end)

Is that the whole script? If not, please show us the whole script.

This is the entire script for the cursor movement.

Try taking the amount that the mouse changes the angle and reverse it (not visually), and then on the next frame put it to the original with the mouse movement changing the angle relative to the reversed CFrame so that you don’t need to make it relative to the camera part CFrame. If you don’t understand this let me know. I can rewrite your script soon if you would like.

Can you try to rewrite my script and explain to me what you meant? Much appreciated.

Yeah, you’ll have to wait a bit though.

This code appears to work for me. Try it and tell me what happens.

-- Variables
local cam = workspace.CurrentCamera
local camPart = workspace.CamPart
local mouse = game:GetService("Players").LocalPlayer:GetMouse()

-- Set Camera
repeat
	task.wait()
	cam.CameraType = Enum.CameraType.Scriptable
until
cam.CameraType == Enum.CameraType.Scriptable

-- Move cam
local maxTilt = 10
local prevAng = nil
game:GetService("RunService").RenderStepped:Connect(function()
	local baseCF = camPart.CFrame
	if prevAng ~= nil then baseCF = CFrame.new(cam.CFrame.Position) * CFrame.Angles(prevAng.Rotation.X,prevAng.Rotation.Y,prevAng.Rotation.Z) end
	cam.CFrame = baseCF * CFrame.Angles(
		math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),
		math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),
		0
	)
	prevAng = cam.CFrame
end)
1 Like

I’ll try it out when I get out of classes. Few questions though, do I replace the old script with this one? If so, will this solve the problem with the tweening?

Alright. Replace this entire code block:

-- Variables
local cam = workspace.CurrentCamera
local camPart = workspace.CamPart
local mouse = game:GetService("Players").LocalPlayer:GetMouse()

-- Set Camera
repeat
	wait()
	cam.CameraType = Enum.CameraType.Scriptable
until
cam.CameraType == Enum.CameraType.Scriptable

-- Move cam
local maxTilt = 10
game:GetService("RunService").RenderStepped:Connect(function()
	cam.CFrame = camPart.CFrame * CFrame.Angles(
		math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),
		math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),
		0
	)
end)

with the code I provided. The code shown here is YOUR code. You replace this with mine that I showed in my previous post. Also, to answer the latter question, the code I showed you earlier should solve the problem with the tweening. I tested out camera tweening in my test place with this and it worked fine.