The issue is likely due to how the target detection is handling small or specific parts. Here's an enhanced version of your findTarget function with improved detection for small objects like the "mobile" part:
local function findTarget()
local origin, direction = getTargetLocation()
if not origin or not direction then return nil end -- Validate ray origin/direction
-- 1. First try precise raycast
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = CollectionService:GetTagged("Draggable")
raycastParams.FilterType = Enum.RaycastFilterType.Include
raycastParams.IgnoreWater = true -- Optional: Add if water is involved
-- Use higher precision raycast first
local maxDistance = 100 -- Increased range for better reach
local raycastResult = workspace:Raycast(origin, direction * maxDistance, raycastParams)
if raycastResult and raycastResult.Instance then
local hitPart = raycastResult.Instance
-- Verify it's actually draggable (double-check)
if DragHandler.IsDraggable(hitPart) then
print("Raycast hit:", hitPart.Name)
return hitPart
end
end
-- 2. Enhanced fallback for small parts
if not UserInputService.GamepadEnabled then
local mouse = Player:GetMouse()
local mousePos = Vector3.new(mouse.X, mouse.Y, 0)
local viewportPoint = Camera:ScreenPointToRay(mousePos.X, mousePos.Y)
-- Create a larger search area
local searchRadius = 5 -- Increased from 2 for small parts
local region = Region3.new(
viewportPoint.Origin - Vector3.new(searchRadius, searchRadius, searchRadius),
viewportPoint.Origin + Vector3.new(searchRadius, searchRadius, searchRadius)
)
-- Get all parts in region
local partsNearCursor = workspace:FindPartsInRegion3(region, nil, 100) -- 100 max parts
-- Sort by distance to camera (closest first)
table.sort(partsNearCursor, function(a, b)
local distA = (a.Position - origin).Magnitude
local distB = (b.Position - origin).Magnitude
return distA < distB
end)
-- Check each part
for _, part in ipairs(partsNearCursor) do
if DragHandler.IsDraggable(part) then
print("Fallback found:", part.Name)
return part
end
end
end
return nil
end
Key improvements made:
- Increased Raycast Range - Max distance boosted to 100 studs
- Better Fallback Search -
- Larger search area (5x5x5 instead of 2x2x2)
- Sorts parts by distance to camera
- Checks up to 100 parts in the region
- Validation - Added checks for valid origin/direction
- - Explicit check in both raycast and fallback