How can I detect when the mouse is clicked in a plugin?

I’m trying to get a position in 3D space for my plugin, which would then become the new position of a part. I managed to do a preview fine, but print("Clicked!") never actually runs, which tells me that for some reason Button1Down is never detected.

I tried putting in the wait(0.1) as a way of not mixing up the clicking of an UI element from my plugin interface with the actual confirmation of position.

local Mouse = plugin:GetMouse()
ActiveSeeking = false

trackCreatorGui.Center.Create.MouseButton1Click:Connect(function()
	local newTrack = newTrackInstance()
	
	wait(0.1)
	coroutine.wrap(beginSeekingPos)(newTrack)
	print("test")

-- <THIS IS THE BIT THAT DOESN'T WORK>
	Mouse.Button1Down:Connect(function()
		print("Clicked!")
		ActiveSeekingPos = false
		
		print(Mouse.Hit.p)
		createNode(Vector3.new(gridRound(Mouse.Hit.p.X, 1), Mouse.Hit.p.Y + 0.5, gridRound(Mouse.Hit.p.Z, 1)), newTrack)
	end)
-- <THIS IS THE BIT THAT DOESN'T WORK/>
end)
The beginSeekingPos function, which is just visual and shouldn't affect the code but I included it in anyway
local function beginSeekingPos(trackInstance)
	
	ActiveSeekingPos = true
	
	local previewItem = Instance.new("Part")
	previewItem.Name = "NodePreview"
	decorateNode(previewItem, Color3.new(0.6,0.6,0.6))
	
	Mouse.TargetFilter = previewItem
	
	while ActiveSeekingPos == true do
		previewItem.Position = Vector3.new(gridRound(Mouse.Hit.p.X, 1), Mouse.Hit.p.Y + previewItem.Size.Y/2, gridRound(Mouse.Hit.p.Z, 1))
		wait()
	end
end

Any help here? Thanks in advance!

Have you activated the plugin first using plugin:Activate()? The plugin needs to be set to activated before it can use the mouse.

Also don’t forget to deactivate your plugin with plugin:Deactivate() once the mouse and/or plugin doesn’t need to be used anymore.

3 Likes