The fact is that you use StarterPlayer, and it changes the camera only after respawn, in order to change the camera at a particular moment, you need to use:
For Local Script
local Player = game.Players.LocalPlayer
Player.CameraMaxZoomDistance = 20
task.wait(10)
Player.CameraMaxZoomDistance = .5
For Server Script
Create a RemoteEvent and put it in ReplicatedStorage and name it CameraEvent
Create a server script in Workspace or ServerScriptService and put this in there
local CameraEvent = game.ReplicatedStorage:WaitForChild("CameraEvent")
CameraEvent:FireAllClients(20) -- Instead of 20, enter the number of maximum camera distance you need
task.wait(10)
CameraEvent:FireAllClients(0.5) -- Instead of 0.5, enter the number of maximum camera distance you need
Create a local script and put it in StarterGui or StarterPlayerScripts and type this in there:
local CameraEvent = game.ReplicatedStorage:WaitForChild("CameraEvent")
local Player = game.Players.LocalPlayer
CameraEvent.OnClientEvent:Connect(function(Focus)
Player.CameraMaxZoomDistance = Focus
end)
If you want smooth zoom then paste this in Local Script:
local TweenService = game:GetService("TweenService")
local CameraEvent = game.ReplicatedStorage:WaitForChild("CameraEvent")
local Player = game.Players.LocalPlayer
CameraEvent.OnClientEvent:Connect(function(Focus)
TweenService:Create(Player, TweenInfo.new(.7, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {
CameraMaxZoomDistance = Focus
}):Play()
end)
Instead of CameraMaxZoomDistance, you can try and use game.Players.LocalPlayer.CameraMode = Enum.CameraMode.LockFirstPerson in a LocalScript. (I would put it in StarterCharacterScripts)