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

What's wrong with my ontouch first person camera script?

Asked by 8 years ago

Hello, Im trying to make first person camera script ontouch, but don't work. Please help me with it.

function OnTouch()
    local player = game.Players.LocalPlayer
    player.CameraMode = 0
    wait()
    player.CameraMode = 1
end

script.Parent.Touched:connect(OnTouch)

0
If i put it to StarterGui works fine, but then i can't click another guis. DevKarolus1 70 — 8y
0
In order to click other GUIs in first person, you need to use the Modal property of a GuiObject (E.G: TextLabel, TextButton, etc.) Note that the mouse will be unlocked until the Modal property is false or if the GuiObject is not visible. Spongocardo 1991 — 8y

1 answer

Log in to vote
1
Answered by
Marios2 360 Moderation Voter
8 years ago

The Players service has a very useful function called GetPlayerFromCharacter. That function allows you to find out wether or not a Player touched your brick, and who it was, by checking a Model and connecting said Model to an existing Player.

You can use it in a server script inside your part:

function OnTouch(part) --The Touched event also provides what part was touched. In this case we use "part" to refer to the touched part, but it can be anything.
    local player = game.Players:GetPlayerFromCharacter(part.Parent) --The function used. Make sure it targets the player's model, not just an individual part.
    if player ~= nil then --We check if there indeed is a player controlling that part.
        player.CameraMode = "LockFirstPerson" --Or you can use Enum.CameraMode.LockFirstPerson
    end
end

script.Parent.Touched:connect(OnTouch)
0
I also believe that "OnTouch" needs to be "onTouch" minikitkat 687 — 8y
0
That doesn't change anything. Marios2 360 — 8y
0
But in lobby can i turn off first person mode? or i need to make model with textbutton? DevKarolus1 70 — 8y
0
You can always switch CameraMode, like most of the properties that aren't read-only. Just use a for _,player in pairs(game.Players:GetPlayers()) do loop for toggling third person back. Any other questions you may have must be made on a new question. Marios2 360 — 8y
0
Ok thanks for help. DevKarolus1 70 — 8y
Ad

Answer this question