Weird camera glitch after tween [SOLVED]

Hello! So I was recently just working on my game, and for whatever reason, a weird camera glitch (that wasn’t there before) just suddenly appeared. I tried to fix it for the past 2 hours but no luck. Here’s what happens:

I’ve been trying for a bit and I narrowed some of it down:
The part (from what I can tell) gets teleported to a really far away place and destroyed. Idk how this even happens, as there’s no deletion code. This isn’t just for this, either, it’s for like half of the cutscenes (one I made 6 months ago without issue). I’m really confused on what’s happening.

Part is anchored, can collide off, never goes unanchored, just teleports or smth

Camera script:

task.wait()
local camera = workspace.CurrentCamera

camera.CameraType = Enum.CameraType.Scriptable

local plr = game.Players.LocalPlayer
local char = plr.Character
local mouse = plr:GetMouse()

local screenshake = false
local screenshakepower = 0

local campart = game.Workspace:WaitForChild("CamPart")
local maxTilt = 5
local ts = game:GetService("TweenService")
local angle = "front"

local previousposition = Vector3.new(0,0,0)

local cando = true

local ismonster1 = false

local anglevalues = {
	["right"] = Vector3.new(0,1.5,10),
	["left"] = Vector3.new(0,1.5,-10),
	["front"] = Vector3.new(-10,1.5,0),
	["top"] = Vector3.new(-2,15,0),
	["back"] = Vector3.new(10,1.5,0),
	["angledfront"] = Vector3.new(-10,7,0),
	["birds"] = Vector3.new(-2,25,0)
}
	
game:GetService("RunService").RenderStepped:Connect(function(deltatime)
	local offset = Vector3.new(0,0,0)
	local humroot = char:FindFirstChild("HumanoidRootPart")
	if cando == true then
		local asd = 0
		camera.FieldOfView = 72.5
		offset = (anglevalues[angle])
		local crouchscript = game.Players.LocalPlayer.Character:FindFirstChild("CrouchScript")
		if crouchscript ~= nil then
			if crouchscript.Crouch.Value == true then
				offset = offset - Vector3.new(0,2,0)
			end
		end
	end
	local previous100fpsvalues = {}
	
	local fps = 1 / game:GetService("RunService").RenderStepped:wait()
	
	table.insert(previous100fpsvalues,fps)
	
	if #previous100fpsvalues > 100 then
		table.remove(previous100fpsvalues,previous100fpsvalues[10])
	end
	
	local averagefps = 0
	
	for i,v in ipairs(previous100fpsvalues) do
		averagefps = averagefps + v
	end
	
	averagefps = averagefps / #previous100fpsvalues
	
	local valuee = 0.08 * (deltatime * (60 + averagefps))
	
	if screenshake == true then
		print("changed")
		offset = offset + Vector3.new(math.random(screenshakepower * -1, screenshakepower),math.random(screenshakepower * -1, screenshakepower),math.random(screenshakepower * -1, screenshakepower))
	end
	if cando == true then
		if humroot ~= nil then
			if ismonster1 == false then
				local twen = ts:Create(campart, TweenInfo.new(valuee,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut), {CFrame = CFrame.new((humroot.Position + offset), humroot.Position)})
				camera.FieldOfView = 70
				twen:Play()
			else
				local twen = ts:Create(campart, TweenInfo.new(valuee,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut), {CFrame = CFrame.new((humroot.Position + (offset + Vector3.new(0,1.5,0))), game.Workspace.CameraLookAt.Position)})
				camera.FieldOfView = 105
				twen:Play()
			end
		end
	else
		if screenshake == true then
			local twen = ts:Create(campart, TweenInfo.new(valuee,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut), {Position = previousposition + offset})
			twen:Play()
		end
	end
	camera.CFrame = campart.CFrame * CFrame.Angles(
		math.rad((((mouse.Y - mouse.ViewSizeY / 1.5)/mouse.ViewSizeY)) * -maxTilt),
		math.rad((((mouse.X - mouse.ViewSizeX / 1.5)/mouse.ViewSizeX)) * -maxTilt),
		0
	)
end)

Cutscene code (Inside camera script; right under the main camera code)


game.ReplicatedStorage.DiscoverMonsterCutscene.OnClientEvent:Connect(function(typee)
	local epic = campart.CFrame
	cando = false
	local twen = ts:Create(campart, TweenInfo.new(1,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out), {CFrame = game.Workspace.SecondMonsterMap.HandPuzzle.Camerae.CFrame})
	twen:Play()
	task.wait(3)
	local twen = ts:Create(campart, TweenInfo.new(1,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out), {CFrame = epic})
	twen:Play()
	cando = true
end)

Idk what happened, please help :grinning: I do not wanna give up on this game

4 Likes

Could you add position prints before and after each camera movement to figure out which one is teleporting the camera part below the workspace destroy height?

Id also recommend setting the player camera CFrame instead of using a physical part that can be removed, but thats not important

1 Like

I found the problem, when you set cando to true, this causes the camera to bug out.

youre tweening the campart at the same time as youre running the

DiscoverMonsterCutscene.OnClientEvent

connection

the problem is here:

if cando == true then
		if humroot ~= nil then
			if ismonster1 == false then
				local twen = ts:Create(campart, TweenInfo.new(valuee,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut), {CFrame = CFrame.new((humroot.Position + offset), humroot.Position)})
				camera.FieldOfView = 70
				twen:Play()
			else
				local twen = ts:Create(campart, TweenInfo.new(valuee,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut), {CFrame = CFrame.new((humroot.Position + (offset + Vector3.new(0,1.5,0))), game.Workspace.CameraLookAt.Position)})
				camera.FieldOfView = 105
				twen:Play()
			end
		end
	else
		if screenshake == true then
			local twen = ts:Create(campart, TweenInfo.new(valuee,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut), {Position = previousposition + offset})
			twen:Play()
		end
	end

You will have to make sure this code doesnt run while the tween is happening inside the remote event connection.

Hope this helped, gl with your game it looks cool

3 Likes

sorry i was unable to check this for a while, ill try what you said (and ty)

2 Likes

I did what I think you said. I basically wanted to test if this was the problem by adding a long wait from when cando = false and the tween is played (I also used prints to make sure it was stopping correctly). It still broke at the end though.

1 Like

I just tried stopping the tweens beforehand as well, also didn’t work

Im gonna send this here hoping you can use this to fix the issue

debugImg1

Ive done

if humroot ~= nil then
			if ismonster1 == false then
				print("trying to tween, ismonster1 is false")
				--local twen = ts:Create(campart, TweenInfo.new(valuee,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut), {CFrame = CFrame.new((humroot.Position + offset), humroot.Position)})
				--camera.FieldOfView = 70
				--twen:Play()
			else
				print("trying to tween, ismonster1 is true")
				--local twen = ts:Create(campart, TweenInfo.new(valuee,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut), {CFrame = CFrame.new((humroot.Position + (offset + Vector3.new(0,1.5,0))), game.Workspace.CameraLookAt.Position)})
				--camera.FieldOfView = 105
				--twen:Play()
			end
		end

As well as

game.ReplicatedStorage.DiscoverMonsterCutscene.OnClientEvent:Connect(function(typee)
	local epic = campart.CFrame
	cando = false
	local twen = ts:Create(campart, TweenInfo.new(1,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out), {CFrame = game.Workspace.SecondMonsterMap.HandPuzzle.Camerae.CFrame})
	warn("Moving to RED")
	twen:Play()
	task.wait(1)
	print("start bugging now / Moving to EPIC")
	local twen = ts:Create(campart, TweenInfo.new(1,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out), {CFrame = epic})
	twen:Play()
	cando = true
end)

And these two combined show the issue here

(I added a wait) after 4 seconds it runs the DiscoverMonsterCutscene event, that is indicated by the warn("Moving to RED")

https://gyazo.com/b1a085b57e4370e217057a0f8b115099

Notice there is no bugging, this is because of the disabled tweens in the first block of code

When they are enabled, the game:GetService("RunService").RenderStepped connection is continuously running the tweens, which I believe to be the cause of the flickering

1 Like

I assumed that that was most likely the cause, so I added a lot of print statements exactly where you put yours. I added extra long waits so the tweens that were being played would stop before the new tween (10 seconds) and it still bugged out.

I was able to completely remove the bug in my testing by simply changing

local campart = game.Workspace:WaitForChild("CamPart")

to

local campart = workspace.Camera

This changes the players camera cframe instead of a part, not sure why but the issue was your camera part was being sent

-3.40282347e+38 studs down, destroying the part because of FallenPartsDestroyHeight

One thing to note is your camera movement may not work the same, so some code might have to be adjusted but you should be using workspace.Camera.Cframe in the future anyways

I dont really know how else to help, so cheers and gl

DO NOT GIVE UP on this game, its not over because of some camera issues or hard to fix bugs, if need be, post on the forum again, but with different context and more solutions you’ve tried. There are always more experienced people than myself who may have run across this issue before. Literally do anything but throw in the towel :+1::+1::+1::+1:

1 Like

Thanks for the encoragement! And yes, this did fix it =) (Idk how I forgot that I could just use the camera object instead of a part)

1 Like

But for real though, thanks for that, it’s really what I needed to hear lol

1 Like