Issue with CollectionService

  1. What do you want to achieve? Keep it simple and clear!

I want to add models/parts into the workspace with tags so that the server can use and change them.

  1. What is the issue?

The problem is when I clone an instance into the workspace, the server won’t recognize the part’s tag.

Server
local CollectionService = game:GetService("CollectionService")
local ServerStorage = game:GetService("ServerStorage")

local Spawns = ServerStorage.Spawns

local function findPart(tag)
	local relative = tag:GetAttribute("RelativePlate")
	for _,part in ipairs(tag.Parent:GetChildren()) do
		if part.Name == relative then
			return part
		end
	end
end

local function findTag(tag)
	for _,touchPart in ipairs(tag.Parent:GetChildren()) do
		if touchPart.Name == "TouchPart" then
			CollectionService:AddTag(touchPart,"Touch")
		end
	end
end

local function createSpawn(tag)
	local relativePart = findPart(tag)
	local chosenSpawn = Spawns.Spawn:Clone()
	chosenSpawn.Parent = workspace
	chosenSpawn:PivotTo(tag.CFrame)
	chosenSpawn:MoveTo(relativePart.Position)
	findTag(tag)
end

local function cleanTags(tag)
	for _,touchPart in ipairs(tag.Parent:GetChildren()) do
		if touchPart.Name == "TouchPart" then
			touchPart:SetAttribute("Touched",true)
		end
	end
end

for _,tag in ipairs(CollectionService:GetTagged("Touch")) do
	tag.Touched:Connect(function()
		local touched = tag:GetAttribute("Touched")
		if not touched then
			cleanTags(tag)
			createSpawn(tag)
		end
	end)
end
  1. What solutions have you tried so far?
    I’ve tried manually adding the tag in a pairs loop but it hasn’t worked.
1 Like

Read the documentation, you could use GetInstanceAddedSignal

local function onInstanceAdded(object: Instance)
    print(instance:GetFullName() .. " has been added!")
end

CollectionService:GetInstanceAddedSignal("Touch"):Connect(onInstanceAdded)
2 Likes

Thats strange when I asked roblox assistant about GetInstanceAddedSignal it said it wasn’t actually a method.

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