Take someone out of first person when touched a part

Hi! I was wondering how I would go about making someone go out of first-person when they hit a brick.
Please provide a sample or a developer hub thread.

Example:

script.Parent.Touched:Connect(function()
 --script to take out of first person
end)

Thanks!

3 Likes

I’m going to bed expect a late response! :sweat_smile:

Make it get your player first using :GetPlayerFromCharacter() and then set the CameraMaxZoomDistance to anything you want. for example 0.5 which is the minimum

Edit:

script.Parent.Touched:Connect(function(hit)--runs the function once part is hit
	if hit.Parent:FindFirstChild("Humanoid") then--checks if it finds the humanoid then its an actual player
		game.Players:GetPlayerFromCharacter(hit.Parent).CameraMaxZoomDistance = 1--sets the camera zoom distance to 1.
	end
end)

I am no scripter but in function I am pretty sure you need to put function(hit)

This works perfectly.

script.Parent.Touched:Connect(function(hit)
	local Player = game.Players:FindFirstChild(hit.Parent.Name)
	if Player then
		Player.CameraMinZoomDistance = 10
		wait(1)
		Player.CameraMinZoomDistance = 0.5
	end
end)

This makes the player able to zoom in again.

2 Likes

Server scripts alone would not work. You would need to use a LocalScript for this, and perhaps a RemoteEvent. This is because the Touched event is detected on the server, but the CameraMinZoomDistance property is changed on the client. So in the server, your script would be:

local Remote = game.ReplicatedStorage:WaitForChild("ThirdPersonEvent")

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
    	Remote:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent))
    end
end)

The ‘hit’ parameter would be the part touching the part, which would most likely be a child of the player’s character. But first, we must check if it is a character, by ensuring there is a Humanoid inside of it. If we do not, the script would error. Next, we fire an event to the player by using game.Players:GetPlayerFromCharacter(). Note that ThirdPersonEvent is the name of your RemoteEvent in ReplicatedStorage (make that if you haven’t). Next in a LocalScript in the player’s character, we set his CameraMinZoomDistance:

local Remote = game.ReplicatedStorage:WaitForChild("ThirdPersonEvent")

Remote.OnClientEvent:Connect(function()
    game.Players.LocalPlayer.CameraMinZoomDistance = 5
end)

You would increase the number (above 5) if you wish for them to be panned further out of the camera. I think that’s about right… yes!

1 Like

There is literally no need to over complete it like that, what I did worked perfectly fine.

No, I believe that the CameraMinZoomDistance (and Max) property can only be changed via the client. Using a server-sided script would not work.

1 Like

It worked quite well for me, using a server script.

Use a Remote Event otherwise, the changes should be made client-side otherwise every client will be affected.

 RemoteEvent:FireClient(Player)

This game is single-player so server side scripts wouldn’t really matter because your the only player.

1 Like

Sorry I do not understand what you are trying to say. I did use a RemoteEvent? Or perhaps I misread your question. :sweat_smile: