How to add TouchEnded function to this script?

Hello
I’ve made a 2D Camera Zone where the player’s camera change to a 2D sided view when touching an area (a block named camerapart in workspace)
Everything seems to work but It keeps that view even if the player is not longer touching that certain area. I tried the TouchEnded function but unfortunately I don’t know where to insert it and make it really work. I want the camera go back to normal when the player leaves that zone. Can you guys give me a little help? :frowning:

Here’s the code:

-- define variables 

-- get local player
local player = game.Players.LocalPlayer  

-- get the camera
local camera = workspace.CurrentCamera

-- get the part
local part = game.Workspace.camerapart

-- set debounce to false
local isHit = false



-- set up on touched function
part.Touched:connect(function(hit)

	if not isHit then
		isHit = true

		-- see if it was hit by humanoid 	
		if hit.Parent:FindFirstChild("Humanoid") then

			-- check to see if it was hit by local player
			if hit.Parent.Name == player.Name then

				-- set camera cframe			
				local camera = game.Workspace.CurrentCamera
				local player = game.Players.LocalPlayer

				camera.CameraType = Enum.CameraType.Scriptable

				local targetDistance = 30
				local cameraDistance = -27
				local cameraDirection = Vector3.new(-1,0,0)

				local currentTarget = cameraDirection*targetDistance
				local currentPosition = cameraDirection*cameraDistance

				game:GetService("RunService").RenderStepped:connect(function()
					local character = player.Character
					if character and character:FindFirstChild("Humanoid") and character:FindFirstChild("HumanoidRootPart") then
						local head = character.HumanoidRootPart
						camera.Focus = head.CFrame
						if head:FindFirstChild("FastStart") == nil then
							camera.CoordinateFrame = 	CFrame.new(Vector3.new(head.Position.X, head.Position.Y + 0, head.Position.Z - 0) + currentPosition, 
								Vector3.new(head.Position.X,  head.Position.Y, head.Position.Z - 0) + currentTarget)
						else
							--Lower camera for fast start
							camera.CoordinateFrame = CFrame.new(Vector3.new(head.Position.X, head.Position.Y - 0, head.Position.Z - 0) + currentPosition, 
								Vector3.new(head.Position.X,  head.Position.Y - 0, head.Position.Z - 0) + currentTarget)
						end
					end
				end)





				-- set camera back to normal but idk how yet lol
				game.Workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
				game.Workspace.CurrentCamera.CameraType = "Custom"



				wait()
			end				

		end			



	end
	wait(1)
	isHit = false
end)

TouchEnded works like Touched, except that it fires when a touch is no longer recorded. Be aware that it is a little unreliable, because if you stand still while still being inside of the part, touchended will fire. If you don’t want this to happen, I would recommend using Magnitude to check if the player is near the center of the part instead of TouchEnded.

1 Like