How to activate a click detector without clicking it

How would I activate a click detector WITHOUT actually activating it? I’m trying to use a keycode to activate a lightswitch.

1 Like

Can you elaborate on what you’re trying to do? I didn’t really understand it.

3 Likes

Explain, I don’t understand what you want to achieve.

can you elaborate on what you want to achieve, I assume you are trying to do something like this?

local function doSomething()
	
end

ClickDetector.MouseClick:Connect(doSomething)

game:GetService("UserInputService").InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E then
		doSomething()
	end
end)
1 Like

To make a click detector without using a click detector do the following:

Add a HitBox around the light switch, and do the following in a local script inside StarterGui:


game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local target = game.Players.LocalPlayer:GetMouse().Target
		
		if target.Name == "MouseHitBox" then -- Change to hitbox name
			-- Do your thing, if it's server-sided use remote function / events
		end
	end
end)
1 Like

In that case, you do not want to use a click detector, but rather, a proximity prompt.

Here is an example of using a proximity prompt:

3 Likes

I do think that in your case you should use a proximity prompt. However if you really need to activate the click detector, try adding a BindableEvent called “Click” (or something else) inside of the detector, and then bind the same function to both the click event and the Bindable Event.

Here’s an example:

-- Assuming this script is placed directly inside the ClickDetector
function onClick()
    -- Your code here
end

script.Parent.MouseClick:Connect(onClick)
script.Parent.Click.Event:Connect(onClick)

Now whenever you want to trigger the detector, you can just fire it’s Click BindableEvent. The ClickDetector will still work normally.

Sorry for the bad explanation, but I want to activate a click detector with a keypress while using a click detector, since I’ve already added clickdetectors, but I want an easier way to do it with the scripts I have.

Hey. You sadly can’t remotely fire a clickdetector, but I think david’s answer does what you want it to do.

1 Like

If I’m reading this correctly, you can use a ProximityPrompt and use the Triggered event to run the script. You can set the button in the ProximityPrompt to whatever key you’d like, and it even supports mobile and console devices.