RobloxScripterAI
Discord ↗
Link Copied to Clipboard
×
"Can we improve this script by making sure the camerapart is still attached to the player?"
Yes, we can improve the script by constantly updating the cameraPart's position to the player's position even when the camera is not zoomed in. This ensures that the cameraPart is always at the correct position relative to the player, ready for when the camera zooms in. Here's how to do it: ```lua -- Local script local player = game.Players.LocalPlayer local UserInputService = game:GetService("UserInputService") local RunService = game:GetService("RunService") local TweenService = game:GetService("TweenService") local camera = workspace.CurrentCamera local zoomedIn = false local defaultFieldOfView = 70 local zoomedFieldOfView = 20 local zoomOffset = Vector3.new(0, 5, 0) -- this is the offset value when zoomed in, adjust the Y value to set the height above the player's head local zoomSpeed = 3.5 -- this is the speed at which the camera will zoom in and out -- Create an invisible part for the camera to follow local cameraPart = Instance.new("Part") cameraPart.Transparency = 1 cameraPart.CanCollide = false cameraPart.Anchored = true cameraPart.Parent = workspace -- Wait for the player's character to load local character = player.CharacterAdded:Wait() -- Wait for the HumanoidRootPart to load local humanoidRootPart = character:WaitForChild("HumanoidRootPart") -- Set the initial position of the camera part cameraPart.Position = humanoidRootPart.Position + zoomOffset UserInputService.InputBegan:Connect(function(input) if input.KeyCode == Enum.KeyCode.Z then if zoomedIn then -- Gradually zoom out local zoomOutTween = TweenService:Create(camera, TweenInfo.new(zoomSpeed), {FieldOfView = defaultFieldOfView}) zoomOutTween:Play() local zoomLocation = humanoidRootPart.Position local zoomOutTweenPart = TweenService:Create(cameraPart, TweenInfo.new(zoomSpeed), {Position = zoomLocation}) zoomOutTweenPart:Play() zoomOutTweenPart.Completed:Wait() camera.CameraSubject = character.Humanoid -- set the camera to follow the player zoomedIn = false else -- Gradually zoom in local zoomInTween = TweenService:Create(camera, TweenInfo.new(zoomSpeed), {FieldOfView = zoomedFieldOfView}) zoomInTween:Play() local zoomLocation = humanoidRootPart.Position + zoomOffset cameraPart.Position = zoomLocation cameraPart.CFrame = CFrame.new(zoomLocation, humanoidRootPart.Position) -- make the camera part look at the player camera.CameraSubject = cameraPart -- set the camera to follow the camera part zoomedIn = true end end end) -- Update the cameraPart's position to the offset location every frame RunService.RenderStepped:Connect(function() cameraPart.Position = humanoidRootPart.Position + zoomOffset end) ``` In this updated script, we moved the line that updates the cameraPart's position into the RenderStepped event. This event fires every frame, so the cameraPart's position is constantly updated to the player's position plus the offset, whether the camera is zoomed in or not.