Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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!

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 JuzeyPlayz -83 — 4y
0
Hello. Make sure the LocalScript is inside "StarterPlayer > StarterPlayerScripts". LocalScript's can't just run inside Workspace. youtubemasterWOW 2741 — 4y
0
Wow! Thank you, You just taught me somthing! JuzeyPlayz -83 — 4y
0
No problem! youtubemasterWOW 2741 — 4y
Ad
Log in to vote
-2
Answered by 4 years ago

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)
0
It did not work?? I used it and put this JuzeyPlayz -83 — 4y

Answer this question