Click to view keyhole

I am trying to make a script where clicking on a part (which is the keyhole) to view inside the door. I don’t get any errors when running but it also isn’t working, I am not sure if I typed it correctly, but It’d be great to get some help.

Server script:

local Highlight = script.Parent.Highlight
local TouchBox = script.Parent.TouchBox
local Click = script.Parent.TouchBox.ClickDetector
local Camera = script.Parent.Parent.Parent.Camera
local KeyCamPart = script.Parent.KeyCamPart

	TouchBox.ClickDetector.MouseHoverEnter:connect(function()
		Highlight.Enabled = false
	end)
TouchBox.ClickDetector.MouseHoverLeave:connect(function()
	Highlight.Enabled = false
end)

TouchBox.ClickDetector.MouseClick:Connect(function()
	repeat
		Camera.CameraType = Enum.CameraType.Scriptable
		wait(.1)
	until	Camera.CameraType == Enum.CameraType.Scriptable
	Camera.CFrame = KeyCamPart.CFrame
end)

You cannot alter the camera on the server. It is something controlled by the client, so it should be handled by a Localscript.

Ok, I’ll change it to local script.

It still doesn’t work. Is there something wrong inside my script?

You are trying to get the camera from script.Parent.Parent.Parent.Camera. If you want to get the local player’s camera, use workspace.CurrentCamera instead. Also, make sure that your script is under StarterPlayerScripts, and not ServerScriptService.

If it’s not under StarterPlayerScripts, it will just be a localscript on the server, instead of the client.

The script is inside a part, but i’ll see if I can do StarterPlayerScripts

I wrote a script that does what you want to do, just with a little less details

local camera = workspace.CurrentCamera
local keyhole = game.Workspace.keyhole.ClickDetector
local campart = game.Workspace.camera

keyhole.MouseClick:Connect(function()
	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = campart.CFrame
end)
1 Like

@TheRealFunlucasmoo the script works finally but it seems like the highlight isn’t doing what it’s supposed to do.

I changed all the variables paths by the way.

local Highlight = game.Workspace.Door.KeyholeView.Highlight
local TouchBox = game.Workspace.Door.KeyholeView.TouchBox
local Click = game.Workspace.Door.KeyholeView.TouchBox.ClickDetector
local Camera = game.Workspace.CurrentCamera
local KeyCamPart = game.Workspace.Door.KeyholeView.KeyCamPart

TouchBox.ClickDetector.MouseHoverEnter:connect(function()
	Highlight.Enabled = false
end)
TouchBox.ClickDetector.MouseHoverLeave:connect(function()
	Highlight.Enabled = false
end)

TouchBox.ClickDetector.MouseClick:Connect(function()
	repeat
		Camera.CameraType = Enum.CameraType.Scriptable
		wait(.1)
	until	Camera.CameraType == Enum.CameraType.Scriptable
	Camera.CFrame = KeyCamPart.CFrame
end)

This script says that when you hover over it it sets enabled to false, just change the first one to say true instead.

I didn’t see that honestly, my mistake

That’s ok, it happens to everybody!

Well, everything works as it should be. Thanks for the help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.