How to fix this camera glitch?

Hi, my name is shieldmr3, I’m a scripter and a game developer.

What’s the issue I’m having? I have a script that makes the camera move as the mouse moves, it’s a main menu. Here’s the script:

local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()
local Character = Player.Character or Player.CharacterAdded:Wait()
local ScreenGui = Player.PlayerGui:WaitForChild("PlayOrNo")
local done = false

local TweenService = game:GetService("TweenService")

local camera = game.Workspace.CurrentCamera



local function tween(part1, part2, TimeToStop)
	local tweenInfo = TweenInfo.new(
		TimeToStop,
		Enum.EasingStyle.Sine,
		Enum.EasingDirection.InOut,
		0,
		false,
		0
	)

	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = part1.CFrame

	local Tween = TweenService:Create(camera, tweenInfo, {CFrame = part2.CFrame})
	Tween:Play()

	wait(TimeToStop)

	print("Got here")
	
	local originalCFrame = camera.CFrame
	local ScaleFactor = 1000
	
	game:GetService("RunService").RenderStepped:Connect(function()
		
		local centreOfScreen = Vector2.new(camera.ViewportSize.X / 2, camera.ViewportSize.Y / 2)
		
		local mouseDistanceFromCentre = Vector3.new((-mouse.X - centreOfScreen.X) / ScaleFactor, (mouse.Y - centreOfScreen.Y) / ScaleFactor, 0)
		
		camera.CFrame = originalCFrame * CFrame.new(originalCFrame.LookVector + mouseDistanceFromCentre)
	end)
	
	done = true
end

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(hit)
	game.Workspace.cam:Destroy()
	tween(game.Workspace.Test1, game.Workspace.Test2, 13)
	while wait() do
		if done then
			ScreenGui.Enabled = true
		end
		break
	end
end)


game.ReplicatedStorage.CamChange.OnClientEvent:Connect(function(plr)
	ScreenGui.Enabled = false
	camera.CFrame = Character.Head.CFrame
	camera.CameraType = Enum.CameraType.Custom
	print("Custom camera!")
end)

When the player presses the play button, it makes the camera type Custom but it stays there, and the camera doesn’t come back to the player. So how can I fix this?

I would really appreciate it if you can help me!

Thanks,
shieldmr3

Note: I haven’t worked with camera manipulation for bit

On returning to normal make sure you also set the cameras subject, pretty sure this is the issue though I may be incorrect here.

I have tried setting the camera subject to Humanoid, but it didn’t do anything, so I tried setting it to the player’s head, and it gave me this error:
image

And to confirm, there is nothing else attempting to set the camera? Can you possibly provide a gif of the cameras behavior when it’s set back?

I’ll send you the video right now!

Don’t have to send it, I found the issue, or so I think. Misreviewed the code and missed the render stepped you set up.

You have setup a RenderStepped function in the original tween function which is still running and is never disconnected. This is overriding the regular camera behavior. To fix this simply create a variable for the event and disconnect it in the CamChange remote function.

How can I disconnect it? Sorry, I don’t really know a lot about run services.

Sorry, I had to delete the video because it doesn’t show the problem, I think that the DevForums removed it.

Here you go!

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local TweenService = game:GetService("TweenService")

local mouse = player:GetMouse()
local screenGui = player.PlayerGui:WaitForChild("PlayOrNo")
local camera = game.Workspace.CurrentCamera

local cameraRenderEvent
local done = false

local function tween(part1, part2, TimeToStop)
	local tweenInfo = TweenInfo.new(
		TimeToStop,
		Enum.EasingStyle.Sine,
		Enum.EasingDirection.InOut,
		0,
		false,
		0
	)

	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = part1.CFrame

	local Tween = TweenService:Create(camera, tweenInfo, {CFrame = part2.CFrame})
	Tween:Play()

	wait(TimeToStop)

	local originalCFrame = camera.CFrame
	local ScaleFactor = 1000

	cameraRenderEvent =	game:GetService("RunService").RenderStepped:Connect(function()
		local centreOfScreen = Vector2.new(camera.ViewportSize.X / 2, camera.ViewportSize.Y / 2)
		local mouseDistanceFromCentre = Vector3.new((-mouse.X - centreOfScreen.X) / ScaleFactor, (mouse.Y - centreOfScreen.Y) / ScaleFactor, 0)

		camera.CFrame = originalCFrame * CFrame.new(originalCFrame.LookVector + mouseDistanceFromCentre)
	end)

	done = true
end

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(hit)
	game.Workspace.cam:Destroy()
	tween(game.Workspace.Test1, game.Workspace.Test2, 13)
	while wait() do
		if done then
			screenGui.Enabled = true
		end
		break
	end
end)


game.ReplicatedStorage.CamChange.OnClientEvent:Connect(function(plr)
	screenGui.Enabled = false
	
	if cameraRenderEvent then
		cameraRenderEvent:Disconnect()
	end
	
	camera.CFrame = character.Head.CFrame
	camera.CameraType = Enum.CameraType.Custom
	print("Custom camera!")
end)

Let me know if this works!

1 Like

Ohh, I understand it now! Thank you so much man, I really appreciate it!

1 Like