Using collectionService, how do you add a tag?

Well, firstly, I need an answer to this question. Will collection service be able to still function on to tagged items CLONED from the replicated storage? It doesn’t seem to work for me, it is as if the cloned object wouldn’t work at all.
If not, then how can I tag an object?

5 Likes

Call CollectionService.AddTag to add tag a part with the first argument as the instance to tag and the second argument as the tag name. And yes, I believe the tag behavior does correspond with cloned parts so tags should transfer to a cloned part from the original part.

1 Like

That’s what I did: CS:AddTag(Part, “TheTag”), but it says, “unable to cast value to object”

Are you sure you’re calling it correctly? Do you mind posting your full script here so I can look at it for you?

Just before I do that, can you tag models?

Yes, tags should work with all instances, including models.

1 Like

You can still tag/untag objects which are cloned. (where they’re cloned from doesn’t matter)

Also, when you clone an object tags are preserved.

AddTag

Is Part and instance?

I would be using local cloned = Part:Clone and CS:AddTag(cloned, “blah”), but it wouldn’t work. But, not that I know that it reserves when it clones. Why isn’t it functioning as intended when it is cloned?

@metryy @Halalaluyafail3 Heres a video of my problem:

If you need help with tagging parts more easier, you can just use a plugin somebody has made:

If you want a tutorial on how to use it/example, watch this: (hopefully it doesnt look like im selling out :sweat_smile:)

2 Likes

Yeah, I know, but in the video, when I hovered over the coconut the first time, it printed, but the cloned one didn’t

Ah, I think you have to re-assign it or so, confusing if i say it but:

If you want to detect all objects with a tag, both present and future, use this method to iterate over objects while also making a connection to a signal returned by CollectionService/GetinstanceAddedSignal.

1 Like

So how would i add that to this script(sorry I need these examples, I’ve barely scripted for a year):

for index, Part in pairs(CollectionFood) do
	local inputConnection
	Part.ClickDetector.MouseHoverEnter:Connect(function()
		print('a')
		inputConnection = UIS.InputBegan:Connect(function(key)
			if key.KeyCode == Enum.KeyCode.C then
				local Reference = Part.Parent.Name
				local Pos = game.Players.LocalPlayer.Character.UpperTorso.Position
				PickUp:FireServer(Reference, Part)
				CollectionFood[index] = nil
			end
			if key.KeyCode == Enum.KeyCode.E then
				local HungerAdd = Part.Hunger.Value
				local HealthAdd = Part.Health.Value
				Hunger:FireServer(HungerAdd, HealthAdd, HumanoidHealth, Part)
				CollectionFood[index] = nil
			end
		end)
	end)
	Part.ClickDetector.MouseHoverLeave:Connect(function()
		Part.Instructions.Enabled = false
		if inputConnection then
			inputConnection:Disconnect()
			inputConnection = nil
		end
	end)
	Part.AncestryChanged:Connect(function()
		if inputConnection then
			inputConnection:Disconnect()
			inputConnection = nil
		end
	end)
end
local function FoodAdded(food)

end

for _,food in pairs(CollectionService:GetTagged("YOUR_TAG_HERE")) do
   FoodAdded(food)
end

CollectionService:GetInstanceAddedSignal("YOUR_TAG_HERE"):Connect(FoodAdded)

This should fix it. You can move the code inside the for loop into the Food Added function and it should work. Except that I named it food instead of Part.

I did this but it didn’t work

for index, Part in pairs(CollectionFood) do
	local inputConnection
	Part.ClickDetector.MouseHoverEnter:Connect(function()
		print('a')
		inputConnection = UIS.InputBegan:Connect(function(key)
			if key.KeyCode == Enum.KeyCode.C then
				local Reference = Part.Parent.Name
				local Pos = game.Players.LocalPlayer.Character.UpperTorso.Position
				PickUp:FireServer(Reference, Part)
				CollectionFood[index] = nil
			end
			if key.KeyCode == Enum.KeyCode.E then
				local HungerAdd = Part.Hunger.Value
				local HealthAdd = Part.Health.Value
				Hunger:FireServer(HungerAdd, HealthAdd, HumanoidHealth, Part)
				CollectionFood[index] = nil
			end
		end)
	end)
	Part.ClickDetector.MouseHoverLeave:Connect(function()
		Part.Instructions.Enabled = false
		if inputConnection then
			inputConnection:Disconnect()
			inputConnection = nil
		end
	end)
	Part.AncestryChanged:Connect(function()
		if inputConnection then
			inputConnection:Disconnect()
			inputConnection = nil
		end
	end)
	
	local function FoodAdded(food)
		
	end

	for _,food in pairs(CS:GetTagged("CollectionFood")) do
		FoodAdded(food)
	end
	
	CS:GetInstanceAddedSignal("CollectionFood"):Connect(FoodAdded)
end

I don’t think I worded it wrong, but I said to put the code from your for loop into the FoodAdded function not my code into your for loop.

Sorry if i’m irritating you but it brought up more errors:

local function FoodAdded(Part)
	for index, Part in pairs(CollectionFood) do
		local inputConnection
		Part.ClickDetector.MouseHoverEnter:Connect(function()
			print('a')
			inputConnection = UIS.InputBegan:Connect(function(key)
				if key.KeyCode == Enum.KeyCode.C then
					local Reference = Part.Parent.Name
					local Pos = game.Players.LocalPlayer.Character.UpperTorso.Position
					PickUp:FireServer(Reference, Part)
					CollectionFood[index] = nil
				end
				if key.KeyCode == Enum.KeyCode.E then
					local HungerAdd = Part.Hunger.Value
					local HealthAdd = Part.Health.Value
					Hunger:FireServer(HungerAdd, HealthAdd, HumanoidHealth, Part)
					CollectionFood[index] = nil
				end
			end)
		end)
		Part.ClickDetector.MouseHoverLeave:Connect(function()
			Part.Instructions.Enabled = false
			if inputConnection then
				inputConnection:Disconnect()
				inputConnection = nil
			end
		end)
		Part.AncestryChanged:Connect(function()
			if inputConnection then
				inputConnection:Disconnect()
				inputConnection = nil
			end
		end)
	end
end

for _,food in pairs(CS:GetTagged("CollectionFood")) do
	FoodAdded(food)
end
	
CS:GetInstanceAddedSignal("CollectionFood"):Connect(FoodAdded)

What are the errors? You can’t just say there are some and not show them.

EDIT: You aren’t supposed to have the for loop in FoodAdded. Just the code inside it.

1 Like

One error is that when I pick something up, it gets duplicated. So I get six of them. And then when I drop it, it still doesn’t work.
But I still don’t understand what you mean. Can you do it for me?

local function FoodAdded(Part)
	for index, Part in pairs(CollectionFood) do -- < REMOVE THIS
		local inputConnection
		Part.ClickDetector.MouseHoverEnter:Connect(function()
			print('a')
			inputConnection = UIS.InputBegan:Connect(function(key)
				if key.KeyCode == Enum.KeyCode.C then
					local Reference = Part.Parent.Name
					local Pos = game.Players.LocalPlayer.Character.UpperTorso.Position
					PickUp:FireServer(Reference, Part)
					CollectionFood[index] = nil
				end
				if key.KeyCode == Enum.KeyCode.E then
					local HungerAdd = Part.Hunger.Value
					local HealthAdd = Part.Health.Value
					Hunger:FireServer(HungerAdd, HealthAdd, HumanoidHealth, Part)
					CollectionFood[index] = nil
				end
			end)
		end)
		Part.ClickDetector.MouseHoverLeave:Connect(function()
			Part.Instructions.Enabled = false
			if inputConnection then
				inputConnection:Disconnect()
				inputConnection = nil
			end
		end)
		Part.AncestryChanged:Connect(function()
			if inputConnection then
				inputConnection:Disconnect()
				inputConnection = nil
			end
		end)
	end -- <-- AND THIS
end

It’s pretty simple, you just remove the for loop. I commented the lines you need to remove. The other errors probably don’t have to do with this code. It probably has to do with the PickUp event.

5 Likes