How to check if a drag detector is hovered over/draggable?

Im trying to make a physics based interaction system using the fairly new drag detectors.

My goal here is to make my mouse cursor enlarge when the player CAN drag an object, so basically if the player is looking at a part with a drag detector and in range to drag it. The default roblox cursor changes when a part is draggable so surely there has to be a already implemented method for doing this.

From reading documentation drag detectors don’t seem to have any events other than the three listed in the documentation, those being DragStart, DragContinue and DragEnd.

I have also seen the MouseEntered methods and such, the only problem to this approach though is that it requires me to have the existing instance of the drag detector that should be hovered over, but this is impractical when dealing with potentially hundreds of drag detectors and I can reference every single drag detector in my script and I don’t really want to iterate through all the drag detectors every frame as that seems as if that would cause a massive performance hit.

I have tried using these events but obviously these only work when the player is dragging which isn’t what I want. I have also tried using raycasts but these provide inconsistent results and are quite messy to work with. My goal is quite

Ok, so I found a crude solution although it functions just fine.

Here’s the code

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local mouse = Players.LocalPlayer:GetMouse() -- Get the players mouse
local hovering = false -- Creating a variable for easy access

local function OnHoverEnter(part) -- Function that is called every frame
	if part:FindFirstChildWhichIsA("DragDetector") then -- Check if the mouse target has a drag detector
		hovering = true
		return hovering
	else
		hovering = false
		return hovering
	end
end

local function CheckHover(player)
	local part = mouse.Target 
	if part then
		OnHoverEnter(part)	
	end
end

Im sure this can be optimised but for my purpose it works just fine. I was hoping there was an event for this but hopefully roblox adds that at some point.

The DragDetector inherits events from the ClickDetector, so you can use .MouseHoverEnter and .MouseHoverLeave.

1 Like

I already tackled this in my question.

You can store a variable that points to the current drag detector being hovered over. Then, just reference that variable. This will be more optimal.

Example:

local currentDragDetector = nil

for i, dragDetector in myCoolFolder:GetChildren() do
	dragDetector.MouseHoverEnter:Connect(function()
		currentDragDetector = dragDetector
	end)
	
	dragDetector.MouseHoverLeave:Connect(function()
		if currentDragDetector == dragDetector then
			currentDragDetector = nil
		end
	end)
end

while task.wait() do
	print(currentDragDetector)
end
1 Like

Oh wow, yeah that does work. Well either way both are great alternatives to each other. I appreciate the input aswell!

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