ClickDetector won't work with custom camera

I changed the CFrame of the player’s camera and I have a ClickDetector on a part, but it won’t detect. I have tried multiple solutions on the developer forum but none of them seem to work. Could somebody help me?

local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local clickPart = game.Workspace:WaitForChild("Part")
local clickdetector = game.Workspace.clickPart.ClickDetector
local Plr = game.Players.LocalPlayer
local Char = Plr.Character or Plr.CharacterAdded:Wait()
local Primary = Char:WaitForChild("HumanoidRootPart")


local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local mouse = player:GetMouse()
local camPos = camera.CFrame.Position

local Cam = workspace.CurrentCamera


local StudsY = 20


local function Enable()
    Cam.CameraType = Enum.CameraType.Scriptable
    game["Run Service"].RenderStepped:Connect(function()
        Cam.CFrame = game.Workspace.CamPart.CFrame
         if clickdetector.MouseClick == true then
         Cam.CFrame = CFrame.new(Primary.Position + Primary.CFrame.UpVector*StudsY) * CFrame.Angles(math.rad(-90),0,0)
        end
    end)
end

Enable()
``

i think the clicking distance is based from the HumanoidRootPart, so i would try raising the click Distance

That fixed the ClickDetector, but nothing else in my script seems to work

i could be wrong, but i dont think “clickdetector.MouseClick == true” is a thing.
you would have to do clickdetector.MouseClick:Connect(function()

also having the runService inside of the function may not be the best way to have that set up

1 Like

I changed “clickdetector.MouseClick == true”, but it still doesn’t seem to work

-- Services
local run = game:GetService("RunService")
local players = game:GetService("Players")
-- Objects
local clickPart = workspace:WaitForChild("Part") :: BasePart
local clickDetector = clickPart:WaitForChild("ClickDetector") :: ClickDetector
-- Player & Charcater
local player = players.LocalPlayer
local camera = workspace.CurrentCamera :: Camera
local character = player.Character or player.CharacterAdded:Wait()
local rootPart = character:WaitForChild("HumanoidRootPart") :: BasePart
-- Variables
local enabled = false
local offsetY = 20
-- Click
clickDetector.MouseClick:Connect(function()
	enabled = not enabled
end)
-- RunService
run.RenderStepped:Connect(function()
	if enabled then
		camera.CFrame = CFrame.new(rootPart.Position + rootPart.CFrame.UpVector*offsetY) * CFrame.Angles(math.rad(-90),0,0)
	end
end)

i cleaned the code up, this should work

1 Like

I tried this, but my camera broke

i forgot, swap the runservice with this one:

-- RunService
run.RenderStepped:Connect(function()
	if enabled then
		camera.CameraType = Enum.CameraType.Scriptable
		camera.CFrame = CFrame.new(rootPart.Position + rootPart.CFrame.UpVector*offsetY) * CFrame.Angles(math.rad(-90),0,0)
	end
end)

I tried it, but the same thing still happened:

-- Services
local run = game:GetService("RunService")
local players = game:GetService("Players")
-- Objects
local clickPart = workspace:WaitForChild("Part") :: BasePart
local clickDetector = clickPart:WaitForChild("ClickDetector") :: ClickDetector
-- Player & Charcater
local player = players.LocalPlayer
local camera = workspace.CurrentCamera :: Camera
local character = player.Character or player.CharacterAdded:Wait()
local rootPart = character:WaitForChild("HumanoidRootPart") :: BasePart
-- Variables
local enabled = false
local offsetY = 20
-- Click
clickDetector.MouseClick:Connect(function()
	enabled = not enabled
end)
-- RunService
run.RenderStepped:Connect(function()
	if enabled then
		camera.CameraType = Enum.CameraType.Scriptable
		camera.CFrame = CFrame.new(rootPart.Position + rootPart.CFrame.UpVector*offsetY) * CFrame.Angles(math.rad(-90),0,0)
	end
end)

before we get any further, may i ask what youre exactly trying to achieve

I’m attempting to create a system where clicking on a block will change the player’s camera perspective

do you get any errors? i ran the code on my file and it worked.
afew things i would check for is:
1- make sure that the part with the click detector is the only part in workspace called “Part” or you can change the name.
2- errors.

No, I have not had any errors.

is the script running ?
and where is the script located and what type of script is it

Yes, it is running.
It is a LocalScript in StarterCharacterScripts

sadly since i cant replicate the issue, not much i can do at this point. sorry
you could try restarting studio and hope its just a bug

how did your camera break, I looked at the latest code posted on this post and it seemed to work for me, and by that I mean when i click the part, the camera gives you a birds eye view. If you want to change it back, then you’ll need to add something to do that, but as far as making the camera look down at the player and staying in that angle it works

I’m unsure, I have no idea why it’s broken.

no i asked how is it broken as in please describe what happened

I just fixed it. It works now!