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

How to make the press [f] for first person?

So this code it not working.. its in a local script

local modelMode = true
local player = game.Players.LocalPlayer
local mouse = player:GetMouse() --Getting the player's mouse
if (Key:lower() == "f")then
	game.StarterPlayer.CameraMode.LockFirstPerson
	
end

local modelMode = true
local player = game.Players.LocalPlayer
local mouse = player:GetMouse() --Getting the player's mouse
if (Key:lower() == "f")then
	game.StarterPlayer.CameraMode.Classic
end

2 answers

✓ Accepted answer

Hello.


First of all, Mouse.KeyDown is deprecated, so always use UserInputService.

Second, you are using StarterPlayer, not the actual player itself. To fix this, use game.Players.LocalPlayer.CameraMode.

Third, you must use an Enum. Indexing LockFirstPerson won't work.


Fixed Script:

local UserInputService = game:GetService("UserInputService")

local firstPerson = false

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.F and not gameProcessedEvent then
		if not firstPerson then
			firstPerson = true
			game.Players.LocalPlayer.CameraMode = Enum.CameraMode.LockFirstPerson
		else
			firstPerson = false
			game.Players.LocalPlayer.CameraMode = Enum.CameraMode.Classic
		end
	end
end)

Now to explain the above:

  1. The InputBegan event will fire when an input begins. (Keyboard, mouse, etc.)

  2. Then there's an if statement checking if the input is "F" on the client's keyboard, and if the player isn't in-chat.

  3. There's another if statement checking if the player isn't in first person. If they aren't, it will lock them in first person and set the firstPerson variable to true. If they are in first person, the firstPerson variable will get set to false and the CameraMode will get set to classic.


Cheers!

4 comments

0

Hey man there are no errors but its not working do i need to create an event? or do I need to like go in game (not in studio test??) Please responed

0

Hello. Make sure the LocalScript is inside "StarterPlayer > StarterPlayerScripts". LocalScript's can't just run inside Workspace.

0

Wow! Thank you, You just taught me somthing!

0

No problem!

You should use UserInputService (Available in LocalScipts) instead.

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, isTyping)
	if isTyping then return end
	if input.KeyCode == Enum.KeyCode.F then
		-- Your desired event.
	end
end)

1 comment

0

It did not work?? I used it and put this

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