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
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.
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!
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)