Click and drag detector not detecting when mouse is dragging over frame

-- LocalScript 
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
-- CONFIGURATION
local FRAME_NAMES = {
	"TraceFrame1",
	"TraceFrame2", 
	"TraceFrame3",
	"TraceFrame4",
	"TraceFrame5",
	"TraceFrame6",
	"TraceFrame7",
	"TraceFrame8",
	"TraceFrame9",
	"TraceFrame10",
	"TraceFrame11",
	"TraceFrame12",
	"TraceFrame13",
	"TraceFrame14",
	"TraceFrame15",
	"TraceFrame16",
	"TraceFrame17",
	"TraceFrame18",
	"TraceFrame19",
	"TraceFrame20",
	"TraceFrame21",
	"TraceFrame22",
	"TraceFrame23",
	"TraceFrame24",
	"TraceFrame25",
	"TraceFrame26",
	
	-- Add more frame names as needed
}

local GUI_NAME = "Cookie" 
-- Variables
local isDragging = false
local tracingFrames = {}
-- Find and store all tracing frames
local function setupFrames()
	local gui = Players.LocalPlayer.PlayerGui.TriangleDalgona:WaitForChild("Cookie")
	if not gui then
		warn("GUI '" .. GUI_NAME .. "' not found!")
		return
	end
	for *, frameName in ipairs(FRAME*NAMES) do
		local frame = gui:FindFirstChild(frameName, true) -- true = search descendants
		if frame then
			table.insert(tracingFrames, frame)
			print("Found tracing frame:", frameName)
		else
			warn("Frame '" .. frameName .. "' not found in " .. GUI_NAME)
		end
	end
end
-- Check if mouse is over any tracing frame
local function getFrameUnderMouse()
	local mousePos = UserInputService:GetMouseLocation()
	for _, frame in ipairs(tracingFrames) do
		if frame and frame.Visible then
			local framePos = frame.AbsolutePosition
			local frameSize = frame.AbsoluteSize
			if mousePos.X >= framePos.X and mousePos.X <= framePos.X + frameSize.X and
				mousePos.Y >= framePos.Y and mousePos.Y <= framePos.Y + frameSize.Y then
				return frame
			end
		end
	end
	return nil
end
-- Handle mouse button down
UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local frameUnderMouse = getFrameUnderMouse()
		if frameUnderMouse then
			isDragging = true
			print("Started dragging on:", frameUnderMouse.Name)
			
			frameUnderMouse.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
		end
	end
end)
-- Handle mouse movement while dragging
UserInputService.InputChanged:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.UserInputType == Enum.UserInputType.MouseMovement and isDragging then
		local frameUnderMouse = getFrameUnderMouse()
		if frameUnderMouse then
			print("Dragging over:", frameUnderMouse.Name)
			frameUnderMouse.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
		end
	end
end)
-- Handle mouse button up
UserInputService.InputEnded:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if isDragging then
			print("Drag ended")
			isDragging = false
			
		end
	end
end)
-- Setup frames when script runs
setupFrames()

In this script it detects when the mouse is being dragged over any of the 26 frames and turns them green when it is, but it seems the detection is very buggy and barely ever correctly detects when it is being dragged over, any reason why?

I’ve had very similar issues in the past. I think it works if you use player:GetMouse().X, and player:GetMouse().Y. Also, you should use PlayerGui:GetGuiObjectsAtPosition().

1 Like

Yea I fixed it, the problem was I was using MouseLocation() instead of GetMouse() which didn’t get the correct position of the mouse

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