Locking Player in Third Person Help

Hello, so I’m scripting this system where if you press Q it will lock you in first person but if you press it again it will lock you in third person. I got the first person part done. It leads you to first person. But the third person part doesn’t send you to third person automatically I don’t want to manually zoom out.

Video:

Script one:

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local ThirdPerson = false
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Humanoid = character:WaitForChild("Humanoid")
local Root = character:WaitForChild("HumanoidRootPart")
local Min = player.CameraMinZoomDistance
local Max = player.CameraMaxZoomDistance
local camera = workspace.CurrentCamera
local connection

player.CameraMaxZoomDistance = 0
player.CameraMinZoomDistance = 0

UserInputService.InputBegan:Connect(function(input,processed)
	if not processed then
		if input.KeyCode == Enum.KeyCode.Q then
			if ThirdPerson then
				if connection then
					connection:Disconnect()
					connection = nil
				end
				
				ThirdPerson = false
				
				Humanoid.CameraOffset = Vector3.new(0,0,-1)
				
				player.CameraMode = Enum.CameraMode.LockFirstPerson;
				
				for _, v in pairs(character:GetChildren()) do
					if v:IsA("BasePart") then
						v.LocalTransparencyModifier = v.Transparency
					end
				end
			else
				ThirdPerson = true
				
				player.CameraMaxZoomDistance = 50
				
				player.CameraMode = Enum.CameraMode.Classic
				
				Humanoid.CameraOffset = Vector3.new(1.5,0,-1)

				connection = RunService.RenderStepped:Connect(function()
					if UserInputService.MouseBehavior ~= Enum.MouseBehavior.LockCenter then
						UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
					end

					local pos = Root.Position + Vector3.new(camera.CFrame.LookVector.X, 0, camera.CFrame.LookVector.Z)

					Root.CFrame = CFrame.new(Root.Position, pos)
				end)
			end
		end
	end
end)

Script two:

local char = script.Parent
local RunService = game:GetService("RunService")

char.Humanoid.CameraOffset = Vector3.new(0, 0, -1)

for i, v in pairs(char:GetChildren()) do
	if v:IsA("BasePart") and v.Name ~= "Head" then

		v:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
			v.LocalTransparencyModifier = v.Transparency
		end)

		v.LocalTransparencyModifier = v.Transparency

	end
end

RunService.RenderStepped:Connect(function(step)
	local ray = Ray.new(char.Head.Position, ((char.Head.CFrame + char.Head.CFrame.LookVector * 2) - char.Head.Position).Position.Unit)
	local ignoreList = char:GetChildren()

	local hit, pos = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)

	if hit then
		char.Humanoid.CameraOffset = Vector3.new(0, 0, -(char.Head.Position - pos).magnitude)
	else
		char.Humanoid.CameraOffset = Vector3.new(0, 0, -1)
	end
end)

2 Likes

You can set the Players CameraMaxZoomDistance and their CameraMinZoomDistance be the same value. This will prevent the player from zooming in and out. You can lock a player’s mouse to the center of the screen by using

-- Lock mouse to center of the screen
local UserInputService = game:GetService("UserInputService")
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseIconEnabled = true -- This makes the mouse icon visible
-- Reset mouse behavior to default
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
UserInputService.MouseIconEnabled = true -- This makes the mouse icon visible

Taken from this post:

1 Like