I want to make it so that when I press the right mouse button it teleports my camera to a part called “cam”
I have this script but it does not work:
-- Variables
local player = game.Players.LocalPlayer
local tool = script.Parent
local userInputService = game:GetService("UserInputService")
-- Function to teleport the camera to the Cam part's position
local function teleportCamera()
local camera = game.Workspace.CurrentCamera
local camPart = tool:FindFirstChild("cam") -- Move this line inside the function
if camPart then
camera.CFrame = CFrame.new(camPart.Position)
end
end
-- Function to handle mouse button events
local function onMouseButtonPressed(input)
if input.UserInputType == Enum.UserInputType.MouseButton2 then -- Right mouse button
teleportCamera()
end
end
-- Connect the function to the mouse button pressed event
userInputService.MouseButton1Down:Connect(onMouseButtonPressed)
in order to change the cframe of the camera you need to set its type to scriptable before changing the cframe,
place this line of code before changing the cframe in the teleportCamera function:
local player = game.Players.LocalPlayer
local tool = script.Parent
local userInputService = game:GetService("UserInputService")
-- Function to teleport the camera to the Cam part's position
local function teleportCamera()
local camera = game.Workspace.CurrentCamera
local camPart = tool:FindFirstChild("cam") -- Move this line inside the function
if camPart then
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = CFrame.new(camPart.Position)
end
end
-- Function to handle mouse button events
local function onInput(input)
if input.UserInputType == Enum.UserInputType.MouseButton2 then -- Right mouse button
teleportCamera()
end
end
-- Connect the function to the mouse button pressed event
userInputService.InputBegan:Connect(onInput)
WOW, it worked. Thanks!
would it be possible to know if there’s a way to reset the camera back to where it was with another click, this is intended for a gun aiming down sights.