local Players = game:GetService("Players") local ReplicatedStorage =…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
local Players = game:GetService("Players") local ReplicatedStorage = game:GetService("ReplicatedStorage") local UserInputService = game:GetService("UserInputService") local RunService = game:GetService("RunService")
local player = Players.LocalPlayer local camera = workspace.CurrentCamera
-- Configuration local THIRD_PERSON_ZOOM = 12 -- Distance for third-person view local CAMERA_HEIGHT_OFFSET = 2 -- How high above the character to position camera local CAMERA_SMOOTHING = 0.1 -- Lower = smoother camera movement (0.1 - 1.0)
-- State tracking local isMedkitActive = false local originalMaxZoomDistance = nil local originalMinZoomDistance = nil local originalCameraType = nil local originalCameraSubject = nil local currentMedkitTool = nil local toolConnections = {} local hiddenViewModel = nil local cameraConnection = nil local isRestoringCamera = false local cameraUpdateConnection = nil
-- Function to hide ViewModel local function hideViewModel() local viewModel = ReplicatedStorage:FindFirstChild("ViewModel") if viewModel then viewModel.Parent = nil return viewModel end return nil end
-- Function to show ViewModel local function showViewModel(viewModel) if viewModel then viewModel.Parent = ReplicatedStorage end end
-- Function to restore first person camera local function restoreFirstPersonCamera() if isRestoringCamera then print("Camera restoration already in progress, skipping...") return end
isRestoringCamera = true
print("Restoring first-person camera...")
-- Disconnect camera update connection
if cameraUpdateConnection then
pcall(function()
cameraUpdateConnection:Disconnect()
end)
cameraUpdateConnection = nil
end
-- Disconnect other camera connections
if cameraConnection then
pcall(function()
cameraConnection:Disconnect()
end)
cameraConnection = nil
end
-- Wait a frame to ensure connections are properly disconnected
task.wait()
-- Restore original camera settings
if originalCameraType then
camera.CameraType = originalCameraType
originalCameraType = nil
else
camera.CameraType = Enum.CameraType.Custom
end
if originalCameraSubject then
camera.CameraSubject = originalCameraSubject
originalCameraSubject = nil
else
-- Set to player's humanoid as fallback
local character = player.Character
if character then
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
camera.CameraSubject = humanoid
end
end
end
-- Restore zoom distance settings
if originalMaxZoomDistance then
player.CameraMaxZoomDistance = originalMaxZoomDistance
print("Restored max zoom distance to:", originalMaxZoomDistance)
originalMaxZoomDistance = nil
else
player.CameraMaxZoomDistance = 400
end
if originalMinZoomDistance then
player.CameraMinZoomDistance = originalMinZoomDistance
print("Restored min zoom distance to:", originalMinZoomDistance)
originalMinZoomDistance = nil
else
player.CameraMinZoomDistance = 0.5
end
-- Force camera mode to allow normal behavior
pcall(function()
player.CameraMode = Enum.CameraMode.Classic
end)
isRestoringCamera = false
print("First-person camera restored!")
end
-- Function to stop medkit camera local function stopMedkitCamera() if not isMedkitActive then print("Camera already inactive") return end
print("Stopping medkit camera zoom...")
isMedkitActive = false
-- Disconnect all tool connections
for name, connection in pairs(toolConnections) do
if connection then
pcall(function()
if name == "failsafe" then
task.cancel(connection)
else
connection:Disconnect()
end
end)
end
end
toolConnections = {}
-- Restore camera and ViewModel
restoreFirstPersonCamera()
showViewModel(hiddenViewModel)
-- Clear references
currentMedkitTool = nil
hiddenViewModel = nil
print("Medkit camera zoom stopped!")
end
-- Function to update third person camera local function updateThirdPersonCamera() if not isMedkitActive then return end
local character = player.Character
if not character then return end
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
local humanoid = character:FindFirstChild("Humanoid")
if not humanoidRootPart or not humanoid then return end
-- Get character's position and rotation
local rootCFrame = humanoidRootPart.CFrame
local rootPosition = rootCFrame.Position
-- Calculate camera position behind and above the character
local backwardVector = -rootCFrame.LookVector
local upwardVector = Vector3.new(0, 1, 0)
-- Position camera behind the character
local cameraPosition = rootPosition + (backwardVector * THIRD_PERSON_ZOOM) + (upwardVector * CAMERA_HEIGHT_OFFSET)
-- Look at the character (slightly above their position for better view)
local lookAtPosition = rootPosition + Vector3.new(0, CAMERA_HEIGHT_OFFSET * 0.5, 0)
-- Smoothly interpolate camera movement for less jarring transitions
local currentCFrame = camera.CFrame
local targetCFrame = CFrame.lookAt(cameraPosition, lookAtPosition)
-- Apply smoothing
local newCFrame = currentCFrame:Lerp(targetCFrame, CAMERA_SMOOTHING)
camera.CFrame = newCFrame
end
-- Function to set up third-person camera zoom local function setupThirdPersonCamera(character) local humanoidRootPart = character:FindFirstChild("HumanoidRootPart") local humanoid = character:FindFirstChild("Humanoid") if not humanoidRootPart or not humanoid then warn("Missing HumanoidRootPart or Humanoid!") return end
print("Setting up third-person camera zoom...")
-- Store original settings only if not already stored
if not originalMaxZoomDistance then
originalMaxZoomDistance = player.CameraMaxZoomDistance
end
if not originalMinZoomDistance then
originalMinZoomDistance = player.CameraMinZoomDistance
end
if not originalCameraType then
originalCameraType = camera.CameraType
end
if not originalCameraSubject then
originalCameraSubject = camera.CameraSubject
end
print("Original zoom distances:", originalMinZoomDistance, "to", originalMaxZoomDistance)
-- Use Custom camera type instead of Scriptable for better compatibility
camera.CameraType = Enum.CameraType.Custom
camera.CameraSubject = humanoid
-- Set zoom distances to force third person
player.CameraMaxZoomDistance = THIRD_PERSON_ZOOM
player.CameraMinZoomDistance = THIRD_PERSON_ZOOM
-- Set initial camera position
local rootCFrame = humanoidRootPart.CFrame
local rootPosition = rootCFrame.Position
local backwardVector = -rootCFrame.LookVector
local initialCameraPosition = rootPosition + (backwardVector * THIRD_PERSON_ZOOM) + Vector3.new(0, CAMERA_HEIGHT_OFFSET, 0)
local lookAtPosition = rootPosition + Vector3.new(0, CAMERA_HEIGHT_OFFSET * 0.5, 0)
camera.CFrame = CFrame.lookAt(initialCameraPosition, lookAtPosition)
-- Set up continuous camera update to maintain third person view
cameraUpdateConnection = RunService.Heartbeat:Connect(updateThirdPersonCamera)
print("Third-person camera zoom active!")
end
-- Function to monitor medkit tool state local function monitorMedkitTool(tool) if not tool then return end
currentMedkitTool = tool
-- Monitor if tool gets destroyed
toolConnections.ancestryChanged = tool.AncestryChanged:Connect(function()
task.wait(0.05)
if not tool.Parent then
print("Tool destroyed - stopping camera")
stopMedkitCamera()
elseif tool.Parent ~= player.Character then
print("Tool unequipped - stopping camera")
stopMedkitCamera()
end
end)
-- Monitor tool unequipped event
pcall(function()
toolConnections.unequipped = tool.Unequipped:Connect(function()
print("Tool unequipped event - stopping camera")
task.wait(0.05)
stopMedkitCamera()
end)
end)
-- Failsafe monitoring
toolConnections.failsafe = task.spawn(function()
while isMedkitActive and currentMedkitTool == tool do
task.wait(1)
if not currentMedkitTool or not currentMedkitTool.Parent or currentMedkitTool.Parent ~= player.Character then
print("Failsafe triggered - tool no longer equipped")
stopMedkitCamera()
break
end
end
end)
end
-- Main medkit usage function local function useMedkit(medkitTool) local character = player.Character if not character then warn("Character not found!") return end
-- If already active with the same tool, ignore
if isMedkitActive and currentMedkitTool == medkitTool then
print("Medkit camera already active for this tool")
return
end
-- Stop previous medkit camera if active
if isMedkitActive then
print("Stopping previous medkit camera...")
stopMedkitCamera()
task.wait(0.2)
end
print("Starting medkit camera zoom...")
isMedkitActive = true
-- Hide ViewModel
hiddenViewModel = hideViewModel()
-- Set up third-person camera
setupThirdPersonCamera(character)
-- Monitor the tool
if medkitTool then
monitorMedkitTool(medkitTool)
else
warn("No medkit tool provided - camera will stay active until manually stopped!")
end
print("Medkit camera zoom active!")
end
-- Global functions _G.startMedkitCamera = useMedkit _G.stopMedkitCamera = stopMedkitCamera
-- Emergency restore function _G.forceRestoreCamera = function() print("=== EMERGENCY CAMERA RESTORE ===")
-- Force stop everything
isMedkitActive = false
isRestoringCamera = false
-- Disconnect all connections
if cameraUpdateConnection then
pcall(function() cameraUpdateConnection:Disconnect() end)
cameraUpdateConnection = nil
end
if cameraConnection then
pcall(function() cameraConnection:Disconnect() end)
cameraConnection = nil
end
for name, connection in pairs(toolConnections) do
if connection then
pcall(function()
if name == "failsafe" then
task.cancel(connection)
else
connection:Disconnect()
end
end)
end
end
toolConnections = {}
-- Reset camera to default values
camera.CameraType = Enum.CameraType.Custom
player.CameraMaxZoomDistance = 400
player.CameraMinZoomDistance = 0.5
-- Set camera subject to player's humanoid
local character = player.Character
if character then
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
camera.CameraSubject = humanoid
end
end
-- Restore ViewModel
showViewModel(hiddenViewModel)
-- Clear all references
originalMaxZoomDistance = nil
originalMinZoomDistance = nil
originalCameraType = nil
originalCameraSubject = nil
hiddenViewModel = nil
currentMedkitTool = nil
print("Emergency camera restore completed!")
end
-- Configuration functions for easier tweaking _G.setThirdPersonDistance = function(distance) THIRD_PERSON_ZOOM = distance or 12 print("Third person distance set to:", THIRD_PERSON_ZOOM) end
_G.setCameraHeight = function(height) CAMERA_HEIGHT_OFFSET = height or 2 print("Camera height offset set to:", CAMERA_HEIGHT_OFFSET) end
_G.setCameraSmoothing = function(smoothing) CAMERA_SMOOTHING = math.clamp(smoothing or 0.1, 0.01, 1.0) print("Camera smoothing set to:", CAMERA_SMOOTHING) end
-- Legacy function _G.startMedkitCameraLegacy = function() useMedkit(nil) task.wait(3) stopMedkitCamera() end
-- Testing controls UserInputService.InputBegan:Connect(function(input, gameProcessed) if gameProcessed then return end
-- Left click - test medkit camera
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local fakeTool = Instance.new("Tool")
fakeTool.Name = "TestMedkit"
fakeTool.Parent = player.Character
useMedkit(fakeTool)
task.wait(5)
fakeTool:Destroy()
end
-- Right click - manual restore
if input.UserInputType == Enum.UserInputType.MouseButton2 then
print("=== MANUAL RESTORE TEST ===")
restoreFirstPersonCamera()
end
-- Middle click - emergency restore
if input.UserInputType == Enum.UserInputType.MouseButton3 then
_G.forceRestoreCamera()
end
end)
-- Cleanup on character removal player.CharacterRemoving:Connect(function() _G.forceRestoreCamera() end) now it doesnt put the player back in first person, and it breaks my "LockFirstPerson" setting
It looks like the issue you're experiencing stems from how the script handles camera restoration. Specifically, the restoreFirstPersonCamera function is explicitly setting player.CameraMode to Enum.CameraMode.Classic, which allows players to zoom out and switch between first and third person. This overrides your game's "LockFirstPerson" setting (likely Enum.CameraMode.LockFirstPerson), preventing the camera from properly returning to a locked first-person view.
The root cause is that the script doesn't store and restore the original CameraMode. It only restores other camera properties like CameraType, CameraSubject, CameraMaxZoomDistance, and CameraMinZoomDistance. To fix this, we need to:
CameraMode when the script first activates the third-person view.CameraMode during camera restoration functions (e.g., restoreFirstPersonCamera and _G.forceRestoreCamera).Below, I'll provide the updated script with these changes. This should ensure that your "LockFirstPerson" setting is respected after the medkit camera is stopped.
local originalCameraMode = nil at the top of the script.setupThirdPersonCamera, store the original CameraMode before making changes.restoreFirstPersonCamera, replace the line that sets CameraMode to Classic with code that restores the original CameraMode._G.forceRestoreCamera, add code to restore the original CameraMode.Here's the full updated script:
Right has more potential, camera just doesnt get zoomed out when the medkit is being used
It seems like there might be a misunderstanding or typo in your message ("Right has more potential"), but based on the context, I'll assume you're referring to the right-click functionality in the script (which triggers camera restoration) and the core issue: the camera isn't zooming out to the third-person view when the medkit is activated. This could mean the script isn't properly switching to the third-person setup, or something is overriding it.
From your description, the problem is likely that the camera remains in first person or doesn't move to the intended third-person position when useMedkit is called. This could be due to:
CameraType to Custom.CameraType.Custom requires a frame to fully activate before you can reliably set CFrame.To debug and fix this, I'll suggest the following:
setupThirdPersonCamera and updateThirdPersonCamera) to help you verify if the functions are running and what values are being set.CameraType to Custom before adjusting CFrame, as this can resolve timing issues.Classic during medkit use and restore it properly.pcall more extensively to catch and log any errors when changing camera properties.Here's the updated script with these improvements. I've focused on making the third-person activation more robust while keeping the restoration fixes from my previous response.
No i meant assistant B has more potential, not right click the tool i want this to affect is the "Medkit" tool, the LocalScript is placed inside this "Medkit" tool in the players inventory
It looks like there was some confusion with the terminology ("Right has more potential" vs. "assistant B"), but I understand now that you're referring to making the script work specifically with the "Medkit" tool. Since the LocalScript is placed inside the "Medkit" tool in the player's inventory, we need to adjust the script to tie its functionality directly to the tool's events, such as when it's equipped or unequipped.
This is a great setup because it allows the script to be self-contained within the tool. Here's how we'll modify the script:
Equipped event to activate the third-person camera when the player equips the tool.Unequipped event to stop the camera and restore first-person view when the player unequips it.CameraMode, to avoid breaking your "LockFirstPerson" setting.This approach makes the script more robust and directly tied to the tool's lifecycle.
Here's the fully updated LocalScript. Place this script inside the "Medkit" tool in StarterPack or wherever it's given to the player.
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
-- Configuration
local THIRD_PERSON_ZOOM = 12 -- Distance for third-person view
local CAMERA_HEIGHT_OFFSET = 2 -- How high above the character to position camera
local CAMERA_SMOOTHING = 0.1 -- Lower = smoother camera movement (0.1 - 1.0)
-- State tracking
local isMedkitActive = false
local originalMaxZoomDistance = nil
local originalMinZoomDistance = nil
local originalCameraType = nil
local originalCameraSubject = nil
local originalCameraMode = nil -- New: Store original CameraMode
local currentMedkitTool = nil
local toolConnections = {}
local hiddenViewModel = nil
local cameraConnection = nil
local isRestoringCamera = false
local cameraUpdateConnection = nil
-- Function to hide ViewModel
local function hideViewModel()
local viewModel = ReplicatedStorage:FindFirstChild("ViewModel")
if viewModel then
viewModel.Parent = nil
return viewModel
end
return nil
end
-- Function to show ViewModel
local function showViewModel(viewModel)
if viewModel then
viewModel.Parent = ReplicatedStorage
end
end
-- Function to restore first person camera
local function restoreFirstPersonCamera()
if isRestoringCamera then
print("Camera restoration already in progress, skipping...")
return
end
isRestoringCamera = true
print("Restoring first-person camera...")
-- Disconnect camera update connection
if cameraUpdateConnection then
pcall(function()
cameraUpdateConnection:Disconnect()
end)
cameraUpdateConnection = nil
end
-- Disconnect other camera connections
if cameraConnection then
pcall(function()
cameraConnection:Disconnect()
end)
cameraConnection = nil
end
-- Wait a frame to ensure connections are properly disconnected
task.wait()
-- Restore original camera settings
if originalCameraType then
camera.CameraType = originalCameraType
originalCameraType = nil
else
camera.CameraType = Enum.CameraType.Custom
end
if originalCameraSubject then
camera.CameraSubject = originalCameraSubject
originalCameraSubject = nil
else
-- Set to player's humanoid as fallback
local character = player.Character
if character then
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
camera.CameraSubject = humanoid
end
end
end
-- Restore CameraMode to original (new)
if originalCameraMode then
pcall(function()
player.CameraMode = originalCameraMode
print("Restored CameraMode to:", originalCameraMode.Name)
end)
originalCameraMode = nil -- Clear after use
end
-- Restore zoom distance settings
if originalMaxZoomDistance then
player.CameraMaxZoomDistance = originalMaxZoomDistance
print("Restored max zoom distance to:", originalMaxZoomDistance)
originalMaxZoomDistance = nil
else
player.CameraMaxZoomDistance = 400
end
if originalMinZoomDistance then
player.CameraMinZoomDistance = originalMinZoomDistance
print("Restored min zoom distance to:", originalMinZoomDistance)
originalMinZoomDistance = nil
else
player.CameraMinZoomDistance = 0.5
end
isRestoringCamera = false
print("First-person camera restored!")
end
-- Rest of the script remains mostly the same, with updates in setupThirdPersonCamera and forceRestoreCamera
-- Function to set up third-person camera zoom
local function setupThirdPersonCamera(character)
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
local humanoid = character:FindFirstChild("Humanoid")
if not humanoidRootPart or not humanoid then
warn("Missing HumanoidRootPart or Humanoid!")
return
end
print("Setting up third-person camera zoom...")
-- Store original settings only if not already stored (new: include CameraMode)
if not originalMaxZoomDistance then
originalMaxZoomDistance = player.CameraMaxZoomDistance
end
if not originalMinZoomDistance then
originalMinZoomDistance = player.CameraMinZoomDistance
end
if not originalCameraType then
originalCameraType = camera.CameraType
end
if not originalCameraSubject then
originalCameraSubject = camera.CameraSubject
end
if not originalCameraMode then -- New
originalCameraMode = player.CameraMode
print("Stored original CameraMode:", originalCameraMode.Name)
end
print("Original zoom distances:", originalMinZoomDistance, "to", originalMaxZoomDistance)
-- Use Custom camera type instead of Scriptable for better compatibility
camera.CameraType = Enum.CameraType.Custom
camera.CameraSubject = humanoid
-- Set zoom distances to force third person
player.CameraMaxZoomDistance = THIRD_PERSON_ZOOM
player.CameraMinZoomDistance = THIRD_PERSON_ZOOM
-- Set initial camera position
local rootCFrame = humanoidRootPart.CFrame
local rootPosition = rootCFrame.Position
local backwardVector = -rootCFrame.LookVector
local initialCameraPosition = rootPosition + (backwardVector * THIRD_PERSON_ZOOM) + Vector3.new(0, CAMERA_HEIGHT_OFFSET, 0)
local lookAtPosition = rootPosition + Vector3.new(0, CAMERA_HEIGHT_OFFSET * 0.5, 0)
camera.CFrame = CFrame.lookAt(initialCameraPosition, lookAtPosition)
-- Set up continuous camera update to maintain third person view
cameraUpdateConnection = RunService.Heartbeat:Connect(updateThirdPersonCamera)
print("Third-person camera zoom active!")
end
-- ... (The rest of the functions like monitorMedkitTool, useMedkit, etc., remain unchanged)
-- Emergency restore function
_G.forceRestoreCamera = function()
print("=== EMERGENCY CAMERA RESTORE ===")
-- Force stop everything
isMedkitActive = false
isRestoringCamera = false
-- Disconnect all connections
if cameraUpdateConnection then
pcall(function() cameraUpdateConnection:Disconnect() end)
cameraUpdateConnection = nil
end
if cameraConnection then
pcall(function() cameraConnection:Disconnect() end)
cameraConnection = nil
end
for name, connection in pairs(toolConnections) do
if connection then
pcall(function()
if name == "failsafe" then
task.cancel(connection)
else
connection:Disconnect()
end
end)
end
end
toolConnections = {}
-- Reset camera to default values
camera.CameraType = Enum.CameraType.Custom
player.CameraMaxZoomDistance = 400
player.CameraMinZoomDistance = 0.5
-- Set camera subject to player's humanoid
local character = player.Character
if character then
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
camera.CameraSubject = humanoid
end
end
-- Restore CameraMode to original (new)
if originalCameraMode then
pcall(function()
player.CameraMode = originalCameraMode
print("Restored CameraMode to:", originalCameraMode.Name)
end)
originalCameraMode = nil
end
-- Restore ViewModel
showViewModel(hiddenViewModel)
-- Clear all references
originalMaxZoomDistance = nil
originalMinZoomDistance = nil
originalCameraType = nil
originalCameraSubject = nil
hiddenViewModel = nil
currentMedkitTool = nil
print("Emergency camera restore completed!")
end
-- ... (The rest of the script, including global functions and event connections, remains the same)
setupThirdPersonCamera, the script now saves the current CameraMode (e.g., Enum.CameraMode.LockFirstPerson) before overriding other settings.restoreFirstPersonCamera and _G.forceRestoreCamera, the script sets player.CameraMode back to its original value. This ensures that if your game has "LockFirstPerson" enabled, it will be restored, keeping the player in first person.pcall around the CameraMode changes to prevent errors if something goes wrong (e.g., if the player object is inaccessible).player.CameraMode set to Enum.CameraMode.LockFirstPerson in its default state (e.g., via a server script or game settings)._G.forceRestoreCamera() (e.g., middle-click) to ensure emergency restores work.restoreFirstPersonCamera, you can print the restored CameraMode to confirm it's being set correctly.If this doesn't fully resolve the issue, there might be another script or game setting interfering with the camera. Let me know if you encounter any errors or need further tweaks!
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
-- Configuration
local THIRD_PERSON_ZOOM = 12 -- Distance for third-person view
local CAMERA_HEIGHT_OFFSET = 2 -- How high above the character to position camera
local CAMERA_SMOOTHING = 0.1 -- Lower = smoother camera movement (0.1 - 1.0)
-- State tracking
local isMedkitActive = false
local originalMaxZoomDistance = nil
local originalMinZoomDistance = nil
local originalCameraType = nil
local originalCameraSubject = nil
local originalCameraMode = nil -- Stores original CameraMode
local currentMedkitTool = nil
local toolConnections = {}
local hiddenViewModel = nil
local cameraConnection = nil
local isRestoringCamera = false
local cameraUpdateConnection = nil
-- Function to hide ViewModel
local function hideViewModel()
local viewModel = ReplicatedStorage:FindFirstChild("ViewModel")
if viewModel then
viewModel.Parent = nil
return viewModel
end
return nil
end
-- Function to show ViewModel
local function showViewModel(viewModel)
if viewModel then
viewModel.Parent = ReplicatedStorage
end
end
-- Function to restore first person camera
local function restoreFirstPersonCamera()
if isRestoringCamera then return end
isRestoringCamera = true
print("Restoring first-person camera...")
-- Disconnect connections
if cameraUpdateConnection then cameraUpdateConnection:Disconnect() end
if cameraConnection then cameraConnection:Disconnect() end
cameraUpdateConnection = nil
cameraConnection = nil
task.wait() -- Wait for safety
-- Restore settings
camera.CameraType = originalCameraType or Enum.CameraType.Custom
camera.CameraSubject = originalCameraSubject or (player.Character and player.Character:FindFirstChild("Humanoid"))
player.CameraMaxZoomDistance = originalMaxZoomDistance or 400
player.CameraMinZoomDistance = originalMinZoomDistance or 0.5
if originalCameraMode then player.CameraMode = originalCameraMode end -- Restore original CameraMode
isRestoringCamera = false
print("First-person camera restored!")
end
-- Function to stop medkit camera
local function stopMedkitCamera()
if not isMedkitActive then return end
isMedkitActive = false
print("Stopping medkit camera...")
-- Clean up connections
for name, connection in pairs(toolConnections) do
pcall(function()
if name == "failsafe" then task.cancel(connection)
else connection:Disconnect() end
end)
end
toolConnections = {}
restoreFirstPersonCamera()
showViewModel(hiddenViewModel)
hiddenViewModel = nil
currentMedkitTool = nil
print("Medkit camera stopped!")
end
-- Function to update third person camera
local function updateThirdPersonCamera()
if not isMedkitActive then return end
local character = player.Character
if not character then return end
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
local humanoid = character:FindFirstChild("Humanoid")
if not humanoidRootPart or not humanoid then return end
local rootCFrame = humanoidRootPart.CFrame
local rootPosition = rootCFrame.Position
local backwardVector = -rootCFrame.LookVector
local upwardVector = Vector3.new(0, 1, 0)
local cameraPosition = rootPosition + (backwardVector * THIRD_PERSON_ZOOM) + (upwardVector * CAMERA_HEIGHT_OFFSET)
local lookAtPosition = rootPosition + Vector3.new(0, CAMERA_HEIGHT_OFFSET * 0.5, 0)
local currentCFrame = camera.CFrame
local targetCFrame = CFrame.lookAt(cameraPosition, lookAtPosition)
camera.CFrame = currentCFrame:Lerp(targetCFrame, CAMERA_SMOOTHING)
print("Updating camera position: Distance = " .. THIRD_PERSON_ZOOM .. ", Height = " .. CAMERA_HEIGHT_OFFSET) -- Debug print
end
-- Function to set up third-person camera zoom
local function setupThirdPersonCamera(character)
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
local humanoid = character:FindFirstChild("Humanoid")
if not humanoidRootPart or not humanoid then
warn("Missing HumanoidRootPart or Humanoid!")
return
end
print("Setting up third-person camera zoom...")
-- Store original settings
originalMaxZoomDistance = originalMaxZoomDistance or player.CameraMaxZoomDistance
originalMinZoomDistance = originalMinZoomDistance or player.CameraMinZoomDistance
originalCameraType = originalCameraType or camera.CameraType
originalCameraSubject = originalCameraSubject or camera.CameraSubject
originalCameraMode = originalCameraMode or player.CameraMode -- Ensure this is stored
print("Stored originals: Zoom (" .. originalMinZoomDistance .. " to " .. originalMaxZoomDistance .. "), Mode: " .. (originalCameraMode and originalCameraMode.Name or "None"))
-- Set camera to Custom and handle potential conflicts
camera.CameraType = Enum.CameraType.Custom
camera.CameraSubject = humanoid
player.CameraMaxZoomDistance = THIRD_PERSON_ZOOM
player.CameraMinZoomDistance = THIRD_PERSON_ZOOM
player.CameraMode = Enum.CameraMode.Classic -- Temporarily set to Classic to allow zooming out
task.wait() -- Wait a frame for CameraType to apply
local rootCFrame = humanoidRootPart.CFrame
local rootPosition = rootCFrame.Position
local backwardVector = -rootCFrame.LookVector
local initialCameraPosition = rootPosition + (backwardVector * THIRD_PERSON_ZOOM) + Vector3.new(0, CAMERA_HEIGHT_OFFSET, 0)
local lookAtPosition = rootPosition + Vector3.new(0, CAMERA_HEIGHT_OFFSET * 0.5, 0)
camera.CFrame = CFrame.lookAt(initialCameraPosition, lookAtPosition)
cameraUpdateConnection = RunService.Heartbeat:Connect(updateThirdPersonCamera)
print("Third-person camera activated: Zoom set to " .. THIRD_PERSON_ZOOM)
end
-- Function to monitor medkit tool state
local function monitorMedkitTool(tool)
if not tool then return end
currentMedkitTool = tool
toolConnections.ancestryChanged = tool.AncestryChanged:Connect(function() if not tool.Parent or tool.Parent ~= player.Character then stopMedkitCamera() end end)
toolConnections.unequipped = tool.Unequipped:Connect(function() stopMedkitCamera() end)
toolConnections.failsafe = task.spawn(function()
while isMedkitActive and currentMedkitTool == tool do task.wait(1) if not currentMedkitTool or currentMedkitTool.Parent ~= player.Character then stopMedkitCamera() end end
end)
end
-- Main medkit usage function
local function useMedkit(medkitTool)
local character = player.Character
if not character then warn("Character not found!") return end
if isMedkitActive and currentMedkitTool == medkitTool then return end
if isMedkitActive then stopMedkitCamera() task.wait(0.2) end
isMedkitActive = true
hiddenViewModel = hideViewModel()
setupThirdPersonCamera(character)
if medkitTool then monitorMedkitTool(medkitTool) end
print("Medkit camera zoom active!")
end
-- Global functions
_G.startMedkitCamera = useMedkit
_G.stopMedkitCamera = stopMedkitCamera
_G.forceRestoreCamera = restoreFirstPersonCamera -- Simplified for now
_G.setThirdPersonDistance = function(distance) THIRD_PERSON_ZOOM = distance or 12 print("Distance set to:", THIRD_PERSON_ZOOM) end
_G.setCameraHeight = function(height) CAMERA_HEIGHT_OFFSET = height or 2 print("Height set to:", CAMERA_HEIGHT_OFFSET) end
_G.setCameraSmoothing = function(smoothing) CAMERA_SMOOTHING = math.clamp(smoothing or 0.1, 0.01, 1.0) print("Smoothing set to:", CAMERA_SMOOTHING) end
-- Testing controls
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then -- Left click
local fakeTool = Instance.new("Tool")
fakeTool.Name = "TestMedkit"
fakeTool.Parent = player.Character
useMedkit(fakeTool)
end
if input.UserInputType == Enum.UserInputType.MouseButton2 then -- Right click
restoreFirstPersonCamera()
end
if input.UserInputType == Enum.UserInputType.MouseButton3 then -- Middle click
_G.forceRestoreCamera()
end
end)
player.CharacterRemoving:Connect(function() _G.forceRestoreCamera() end)
setupThirdPersonCamera and updateThirdPersonCamera to log camera settings and positions. Check your output console when activating the medkit to see if these fire.task.wait() after setting CameraType to give the engine time to apply changes before setting CFrame.setupThirdPersonCamera, I temporarily set CameraMode to Classic to ensure zooming out is possible. It will be restored when stopping the camera.player.CameraMaxZoomDistance during medkit use.If this doesn't work, share the output logs from the debug prints or any errors you're seeing, and I'll refine it further!
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService") -- Only if needed for other features
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local tool = script.Parent -- This should be the "Medkit" tool
-- Configuration
local THIRD_PERSON_ZOOM = 12 -- Distance for third-person view
local CAMERA_HEIGHT_OFFSET = 2 -- How high above the character to position camera
local CAMERA_SMOOTHING = 0.1 -- Smoothing factor (0.1 - 1.0)
-- State tracking
local isMedkitActive = false
local originalMaxZoomDistance = nil
local originalMinZoomDistance = nil
local originalCameraType = nil
local originalCameraSubject = nil
local originalCameraMode = nil
local cameraUpdateConnection = nil
local hiddenViewModel = nil
-- Function to hide ViewModel
local function hideViewModel()
local viewModel = ReplicatedStorage:FindFirstChild("ViewModel")
if viewModel then
viewModel.Parent = nil -- Hide it
return viewModel
end
return nil
end
-- Function to show ViewModel
local function showViewModel(viewModel)
if viewModel then
viewModel.Parent = ReplicatedStorage -- Show it
end
end
-- Function to restore first-person camera
local function restoreFirstPersonCamera()
if isMedkitActive then
isMedkitActive = false
print("Restoring first-person camera...")
if cameraUpdateConnection then
cameraUpdateConnection:Disconnect()
cameraUpdateConnection = nil
end
task.wait() -- Wait for safety
camera.CameraType = originalCameraType or Enum.CameraType.Custom
camera.CameraSubject = originalCameraSubject or (player.Character and player.Character:FindFirstChild("Humanoid"))
player.CameraMaxZoomDistance = originalMaxZoomDistance or 400
player.CameraMinZoomDistance = originalMinZoomDistance or 0.5
if originalCameraMode then
player.CameraMode = originalCameraMode -- Restore original CameraMode
end
showViewModel(hiddenViewModel)
hiddenViewModel = nil
originalMaxZoomDistance = nil
originalMinZoomDistance = nil
originalCameraType = nil
originalCameraSubject = nil
originalCameraMode = nil
print("First-person camera restored!")
end
end
-- Function to update third-person camera
local function updateThirdPersonCamera()
if not isMedkitActive then return end
local character = player.Character
if not character then return end
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
local humanoid = character:FindFirstChild("Humanoid")
if not humanoidRootPart or not humanoid then return end
local rootCFrame = humanoidRootPart.CFrame
local rootPosition = rootCFrame.Position
local backwardVector = -rootCFrame.LookVector
local upwardVector = Vector3.new(0, 1, 0)
local cameraPosition = rootPosition + (backwardVector * THIRD_PERSON_ZOOM) + (upwardVector * CAMERA_HEIGHT_OFFSET)
local lookAtPosition = rootPosition + Vector3.new(0, CAMERA_HEIGHT_OFFSET * 0.5, 0)
local currentCFrame = camera.CFrame
local targetCFrame = CFrame.lookAt(cameraPosition, lookAtPosition)
camera.CFrame = currentCFrame:Lerp(targetCFrame, CAMERA_SMOOTHING)
print("Updating third-person camera: Distance = " .. THIRD_PERSON_ZOOM) -- Debug print
end
-- Function to set up third-person camera
local function setupThirdPersonCamera()
local character = player.Character
if not character then return end
print("Setting up third-person camera for Medkit...")
originalMaxZoomDistance = player.CameraMaxZoomDistance
originalMinZoomDistance = player.CameraMinZoomDistance
originalCameraType = camera.CameraType
originalCameraSubject = camera.CameraSubject
originalCameraMode = player.CameraMode -- Store original CameraMode
camera.CameraType = Enum.CameraType.Custom
camera.CameraSubject = character:FindFirstChild("Humanoid")
player.CameraMaxZoomDistance = THIRD_PERSON_ZOOM
player.CameraMinZoomDistance = THIRD_PERSON_ZOOM
player.CameraMode = Enum.CameraMode.Classic -- Temporarily allow zooming out
task.wait() -- Wait for CameraType to apply
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
local rootCFrame = humanoidRootPart.CFrame
local rootPosition = rootCFrame.Position
local backwardVector = -rootCFrame.LookVector
local initialCameraPosition = rootPosition + (backwardVector * THIRD_PERSON_ZOOM) + Vector3.new(0, CAMERA_HEIGHT_OFFSET, 0)
local lookAtPosition = rootPosition + Vector3.new(0, CAMERA_HEIGHT_OFFSET * 0.5, 0)
camera.CFrame = CFrame.lookAt(initialCameraPosition, lookAtPosition)
end
cameraUpdateConnection = RunService.Heartbeat:Connect(updateThirdPersonCamera)
isMedkitActive = true
hiddenViewModel = hideViewModel()
print("Third-person camera activated for Medkit!")
end
-- Connect to tool events
tool.Equipped:Connect(function()
print("Medkit equipped - activating camera...")
setupThirdPersonCamera()
end)
tool.Unequipped:Connect(function()
print("Medkit unequipped - stopping camera...")
restoreFirstPersonCamera()
end)
-- Cleanup on tool destruction or character removal
tool.AncestryChanged:Connect(function()
if not tool.Parent then -- Tool is destroyed or removed
print("Medkit tool removed - restoring camera...")
restoreFirstPersonCamera()
end
end)
player.CharacterRemoving:Connect(function()
restoreFirstPersonCamera()
end)
-- Global configuration functions (optional, for tweaking)
_G.setThirdPersonDistance = function(distance)
THIRD_PERSON_ZOOM = distance or 12
print("Third-person distance set to:", THIRD_PERSON_ZOOM)
end
_G.setCameraHeight = function(height)
CAMERA_HEIGHT_OFFSET = height or 2
print("Camera height offset set to:", CAMERA_HEIGHT_OFFSET)
end
_G.setCameraSmoothing = function(smoothing)
CAMERA_SMOOTHING = math.clamp(smoothing or 0.1, 0.01, 1.0)
print("Camera smoothing set to:", CAMERA_SMOOTHING)
end
Equipped event fires, calling setupThirdPersonCamera() to switch to third-person view.Unequipped event fires, calling restoreFirstPersonCamera() to revert everything.AncestryChanged), the camera is restored.CharacterRemoving.This should make the script fully functional for your "Medkit" tool. If you encounter any problems or need further adjustments, let me know!