Why is my target variable returning nil?

  1. What do you want to achieve? When the player begins dragging an object (the target), I want it to print the name of the object.

  2. What is the issue? Target variable is returning nil and I’m not sure why.

local function assignDragDetector()
	for _, interactableObjects in pairs(workspace.Interactable:GetChildren()) do
		local dragDetector = Instance.new("DragDetector")
		dragDetector.Parent = interactableObjects
		dragDetector.DragStyle = Enum.DragDetectorDragStyle.Scriptable
		
		local function customDragStyle(dragging, target)
			if dragging then
				print(target)
			end
		end
		dragDetector:SetDragStyleFunction(customDragStyle)
	end
end

assignDragDetector()

The customDragStyle function isn’t actually getting the target as a valid part, thus rendering it nil. Instead, you can utilize the dragDetector.DragStart event to find the parts name:

local function assignDragDetector()
	for _, interactableObjects in pairs(workspace.Interactable:GetChildren()) do
		local dragDetector = Instance.new("DragDetector")
		dragDetector.Parent = interactableObjects
		dragDetector.DragStyle = Enum.DragDetectorDragStyle.Scriptable
		
		dragDetector.DragStart:Connect(function(plr, ray, cf, hf, part)
			print(part.Name)
		end)
	end
end

assignDragDetector()
1 Like

Would this still work with the custom drag style? Printing the parts name was just see if it was obtaining the object, which it wasn’t. I was hoping to add more functionality to the drag detectors through the customDragStyle function, not sure if i can still do that with this method.

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