How does this function check if the parent has tag drag?

I am using an extension called tag editor, all these tags do is mark parts and objects. HasTag simply checks if the part put in the parameters has the tag put in the parameters which would be “drag” in this case. When this function runs it checks if the objects parent/parents has the tag “drag”, I do not understand how.

local object = mouse.Target

function getDraggable(object)
	while object ~= workspace do
		if CollectionService:HasTag(object, "drag") then
			return object
		end
		object = object.Parent
	end
	return nil
end

I’m not quite sure what your question is.

That line checks for tags using CollectionService | Roblox Creator Documentation. The plugin you’re using internally is also adding/removing tags using CollectionService.

You don’t need the plugin, it’s just a convenient way to do this:

-- adds "drag" tag to workspace.Part
game:GetService("CollectionService"):AddTag(workspace.Part, "drag")

And/or this:

-- removes "drag" tag from workspace.Part
game:GetService("CollectionService"):RemoveTag(workspace.Part, "drag")

Okay so basically I run this function when I am trying to see if a model has the tag “drag”, someone else wrote this function btw. What is confusing me is how none of the parts inside of the model are tagged with “drag” individually, yet this function is able to check if the parent model has the tag.

Oh I see your question.

It’s checking tags in a loop. Starts with the object you click on, and moves up the hierarchy until it finds one.

It does this by re-assigning the object variable to object.Parent if the current object doesn’t have the Tag.

Here’s the commented version:

-- start with the part you clicked on
local object = mouse.Target

function getDraggable(object)
	-- loop until we reach workspace (the highest parent in the land)
	while object ~= workspace do

		-- check the current "level" for the tag
		if CollectionService:HasTag(object, "drag") then

			-- quit early - we found a tag
			return object
		end

		-- we haven't quit early. so move up one level...
		object = object.Parent

	-- ...and go back to the top of the loop with the new "object"
	end

	-- we'd only get here if the loop finishes without returning,
	-- meaning we reached "workspace" before finding a tag.
	return nil
end
1 Like

Interesting, I see now thanks!

1 Like

The first line local object = mouse.Target is actually not really relevant here.

It’s probably just used later in something like

local draggable = getDraggable(object)

And the author just used the same name (object) for the variable and the argument.

You could just as easily delete the first line and just do

local draggable = getDraggable(mouse.Target)