Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I lock a FPS camera and unlock it?

I'm trying to manipulate the roblox camera to make a FPS. I want the player to be forced to stay in First Person Mode, but i want them to be able to unlock their mouse in FPS mode with a key like for example "Alt". I press Alt and my mouse is free to move. I press Alt again and it locks it self to the center of the screen. This would be useful to click GUIs and Etc in First Person Mode.

I have the basic FPS script:

game.Players.PlayerAdded:connect(function(player)
player.CameraMode = Enum.CameraMode.LockFirstPerson
end)

Please reply/help me! Thank you!

2 answers

LocalScript

local Player=game.Players.LocalPlayer 
local Mode=false

Player:GetMouse().KeyDown:connect(function(key) --Getting PlayerMouse, KeyDown event
	if key==3 or 4 then --Left or Right 'alt' keys
		Mode=not Mode 
		if Mode==true then -- if mode equals true
			Player.CameraMode=Enum.CameraMode.LockFirstPerson
		elseif Mode==false then --if mode equals false
			Player.CameraMode=Enum.CameraMode.Classic
		end
	end
end)

i recommend you use "CameraMaxZoomDistance" instead of LockFirstPerson because is a bit unpredictable(In local script)

local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()
game.Players.PlayerAdded:connect(function(player)
	player.CameraMaxZoomDistance = 0
end)
-- and also have another function that binds it
	Mouse.KeyDown:connect(function(key)
		if key ==  "y" then
			--do something
		end
		

If you want to learn some other things read this : http://wiki.roblox.com/index.php?title=Taking_keyboard_input

This discussion is preserved as it was. New answers, comments, and votes are closed.