✓ 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:
The InputBegan event will fire when an input begins. (Keyboard, mouse, etc.)
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.
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
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
Hello. Make sure the LocalScript is inside "StarterPlayer > StarterPlayerScripts". LocalScript's can't just run inside Workspace.
Wow! Thank you, You just taught me somthing!
No problem!