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

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

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)

2 comments

0

If i put it to StarterGui works fine, but then i can't click another guis.

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.

1 answer

✓ Accepted answer

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)

5 comments

0

I also believe that "OnTouch" needs to be "onTouch"

0

That doesn't change anything.

0

But in lobby can i turn off first person mode? or i need to make model with textbutton?

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.

0

Ok thanks for help.

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