Cloned tagged objects won't work

So I made this gatherable items script where it uses collection service (To avoid individual scripts hassle) and then whenever the prompt inside the tagged objects are triggered, it should remove the items and place it inside the inventory.

But I made chest that when it gets destroyed, it spawns the pickable items around it (The ones with prompts inside it) it does clones the items, however, The prompts won’t trigger. I also noticed that objects that are already placed in the workspace works while the cloned ones doesn’t. I’ll be sending video clips as well as source codes below for more clarification.

Working items in workspace

Below are the items that’s already in the workspace (It works, it gets deleted upon prompt trigger)

1st video source code
local SetTags = CollectionService:GetTagged("PickUp")

local Debounce = false
for _,v in pairs(SetTags) do
	if v:IsA("Model") or v:IsA("BasePart") or v:IsA("MeshPart")  then
		for i, v in pairs(v:GetDescendants()) do	
			if v:IsA("BasePart") or v:IsA("MeshPart") then
				if v:FindFirstChildWhichIsA("ProximityPrompt") then				
					local Prompt = v:WaitForChild("prompt")

					Prompt.Triggered:Connect(function(Player)
						if Debounce == false then
							Debounce = true

							game.Debris:AddItem(v, .1)

							Debounce = false
						end					
					end)
				end		
			end	
		end					
	end		
end
Not working prompts cloned items

While this are the cloned items (It clones the object however, The prompts won’t trigger and get the objects deleted.)

2nd video source code
	if v:IsA("Model") then
		for i,Chest in pairs (v:GetChildren()) do
			if Chest:IsA("BasePart") or Chest:IsA("MeshPart") then				
				local Debounce = false
				
				Chest.Touched:Connect(function(PartCollider)
					if  PartCollider.Parent:FindFirstChildWhichIsA("Humanoid")  then			
						if PartCollider:IsA("BasePart") or PartCollider:IsA("MeshPart") then
							if Debounce == false then
								Debounce = true
																											
								for i = 1,9 do
									wait()
									print("Chest Touched!")-- prints if the Chest is touched
									local ChosenObjects = RandomItems[math.random(1,#RandomItems)]:Clone() -- Clones the items and spawns it around the chest object.
									
									local SetFrame = ChosenObjects:SetPrimaryPartCFrame(Chest.CFrame) -- Setting the primary part CFrame since the one im cloning is a model.
									SetFrame = Chest.CFrame -- Sets the object CFrame to Chest CFrame
									ChosenObjects.Parent = game.Workspace
								end

								v:Destroy()

								Debounce = false
							end
						end	
					end
				end)
			end
		end			
	end					
end

Any help would be really appreciated!

2 Likes

Why are you using CFrame to move the parts?

2 Likes

It’s what im using to spawn objects / move it, Do you have any alternatives I can use?

2 Likes

There’s nothing wrong with that.


@godlyzechter Have you tried re-tagging them after cloning?

1 Like

Haven’t tried it yet since it’s already tagged after cloning, I’ll try re tagging it now for extra measures.

Just tried retagging the cloned objects in this line however, It still won’t work

									wait()
									print("Chest Touched!")-- prints if the Chest is touched
									local ChosenObjects = RandomItems[math.random(1,#RandomItems)]:Clone()
									CollectionService:AddTag(ChosenObjects, "PickUp")
									
									local SetFrame = ChosenObjects:SetPrimaryPartCFrame(Chest.CFrame) -- Setting the primary part CFrame since the one im cloning is a model.
									SetFrame = Chest.CFrame -- Sets the object CFrame to Chest CFrame
									ChosenObjects.Parent = game.Workspace
								end

Oh wait, I see the real problem.

You are getting all the tagged items at the start, but you also need to react to things tagged in the future.

local CollectionService = game:GetService("CollectionService")

local function handleTagged(object: Instance)
    -- end
end

-- this is what you missed
CollectionService:GetInstanceAddedSignal(tag):Connect(handleTagged)

for _, object in ipairs(CollectionService:GetTagged(tag)) do
    handleTagged(object)
end
2 Likes

Do you have some examples on where to use that since I haven’t really mastered collection services yet.

Imagine you have three objects.

- Part1 (tagged with Burning)
- Part2 (tagged with Burning)
- Part3 (not tagged)

You get a list of tagged objects and do stuff to them.
In this example, we get the objects with the tag “Burning” and turn them black.

local CollectionService = game:GetService("CollectionService")

for _, object in ipairs(CollectionService:GetTagged("Burning")) do
    object.Color = Color3.new(0, 0, 0)
end

However, what if we tag Part3?

- Part3 (tagged with Burning)

It won’t be turned black, because the script has already run.
To be able to turn parts that didn’t start out burning but have caught fire in the future, we use an event.
We can get the event with CollectionService:GetInstanceAddedSignal.

local Event = CollectionService:GetInstanceAddedSignal("Burning")
Event:Connect(function(object)
    object.Color = Color3.new(0, 0, 0)
end)
2 Likes

Just tried the algorithm you did as well as experimented some stuffs of it, It works now thanks for taking your time to explain!

2 Likes