Camera Subject Tween issues when switching back from object to player

Hi all.

I am attempting to create a peeking system for doors in my game. by using proximity prompts.

The issue I am experiencing is that the system works fine sometimes, and other times it spazzes out the camera with a black screen and returns the players camera back to their character without letting go of the key input.

There are no errors either. I did some debugging and found that the tween is the issue here, causing the error as trialing by setting cameraSubject to doorOne without the tween works as normal.

See below.

RobloxStudioBeta_lFKtgXyOAI

– Local Script

for i, v in pairs(workspace.DoorFolder:GetChildren()) do
	local proximityPromt = v.Door.ProximityPrompt
	if proximityPromt then

		proximityPromt.Triggered:Connect(function()
			print("hold began")
			local doorOne = v.DoorOne
			local doorTwo = v.DoorTwo
			Viewer.SetCF(doorOne, doorTwo.Position)

			camera.CameraType = Enum.CameraType.Scriptable

			local doorRotationTweenInfo = TweenInfo.new(0.5)
			local goal = {CFrame = doorOne.CFrame}
			goal.CFrame = CFrame.new(doorOne.Position.X, doorOne.Position.Y, doorOne.Position.Z)

			local tween = TweenService:Create(camera, doorRotationTweenInfo, goal)
			tween:Play()
		end)

		
		proximityPromt.TriggerEnded:Connect(function()
			print("hold ended")
			print("Let go.")
			camera.CameraType = Enum.CameraType.Follow
			if Player.Character then
				camera.CameraSubject = Player.Character:FindFirstChild("Humanoid")
			end
		end)
	end
end


-- viewer module script

```lua
local Viewer = {}
local Camera = workspace.CurrentCamera

function CFrameMatrix(position, lookat)
	local lookVector = (position - lookat).Unit
	local mUpVec = Vector3.new(0, 1, 0)
	local rightVec = lookVector:Cross(mUpVec)
	local upVec = rightVec:Cross(lookVector)
	return CFrame.fromMatrix(lookat, rightVec, upVec)
end


function Viewer.SetCF(C1, POS1)
	C1.CFrame = CFrameMatrix(C1.Position, POS1)
end

return Viewer

Any help would be appreciated.

2 Likes

im pretty sure it’s because the tween continues to play even when cancelled
you gotta store the tween into a variable that can be used by both functions

proximityPromt.TriggerEnded:Connect(function()
Tween:Cancel()

1 Like