CollectionService in a nutshell

Well my problem is that how do I run this with a object that is added to the game while the for loop is running like if I already have kill parts then I add another one with the tag it won’t work. How would I do that??

What I do in my projects is making a function called “UpdateTags” so everytime I know a new part is added I fire that function which fires again the for loop to connect all the new part/s.

1 Like

CAn you show me an example of this and also I don’t want mine to fire again for the parts already tagged.

local CollectionService = game:GetService("CollectionService")

local TagNames = {
	PadTag = "PadObject",
	JumpPad = "JumpPad",
	SpeedPad = "SpeedPad",
	MagnetPad = "MagnetPad",
}

local TagHandler = {}

function TagHandler.UpdateTags(group)
	if (typeof(group) == "Instance") then
		for _, object in pairs(group:GetDescendants()) do
			if (object:IsA("BasePart") and object:FindFirstChild("PadConfiguration")) then
				CollectionService:AddTag(object, TagNames.PadTag)

				if (object.PadConfiguration:FindFirstChild("Type") and type(object.PadConfiguration.Type.Value) == "string") then
					for _, name in pairs(TagNames) do
						if (object.PadConfiguration.Type.Value:match(name)) then
							CollectionService:AddTag(object, object.PadConfiguration.Type.Value)
						end
					end
				end
			end

			if (object:IsA("BasePart") and object:FindFirstChild("CanKill") and object.CanKill:IsA("BoolValue")) then
				CollectionService:AddTag(object, "CanKill")
			end
		end
	end
end

return TagHandler

That’s the code that I use in my project, basically what the code does is checking all the parts and looking into them for some sort of value with a name, if the script finds the value then that part is tagged.

An easier example is something like this:

function UpdateTagFunction(Tag)
   if Tag == "killBrick" then
       for _, part in pairs(game:GetService("CollectionService"):GetTagged(Tag)) do
          part.Touched:Connect(...)
      end
   end
end

If you have multiple tags you can do

function UpdateTagFunction(Tag)
   if Tag == "killBrick" then
      for _, part in pairs(game:GetService("CollectionService"):GetTagged(Tag)) do
          part.Touched:Connect(...)
      end
   elseif Tag == "otherTag" then
       --other thing
   end
end

Then you must create some sort of array containing the parts you recently tagged and only tag & connect the parts into the array

3 Likes

Thanks for everything… I’ll use this to fix my stuff.

do tag changes replicate client → server?

Tags will only replicate from the server to client, not from client to server.

4 Likes

This was one explanation amongst many that I fully understood, thank you very much!

1 Like

Actually, his grammer was amazing considering hes from spain. Loved the post!!!

3 Likes

I feel like this module will be really helpful for some of my game mechanics!

This isn’t a module btw, it’s a service CollectionService | Roblox Creator Documentation.

3 Likes

image

Great tutorial! I was able to set this up easily for parts, but is it possible to use CollectionService for local scripts I put inside StarterGui? i.e. Within a player’s GUI there are exit buttons in multiple places with a local script that closes all screens. I’ve tagged all of these exit button instances, but I don’t understand what the next steps would be since all the examples I can find online only deal with server scripts. It doesn’t seem like the structure above would work if “killing brick script” was local right?

2 Likes

It should just be like a server script.

1 Like

Tags don’t replicate to the server, only from. Checking them client side is not an issue.

2 Likes

Tags can be added by the Server and the Client, but only server-made ones will replicate to all clients meaning that you could and should use a LocalScript for GUI tagging since they can only be seen and accessed by a specific client.

This is the replication scheme:

Server → :AddTag() → Tag is added → Tag replicates to other clients
Client → :AddTag() → Tag is added

Server-made tags can be accessed by: Server, Client, Other Clients
Client-made tags can be accessed by: Client

It would, but only for the client that was in control or had ownership of the LocalScript. In that case, the client would see them as tagged bricks and would die if it was to touch them, but other players would not die since they won’t even see the tag in a first place.

3 Likes

I would add, for security of our games, be very careful what you let the client monitor with regards to tags. For example, I add a GUID tag to all spawned items to prevent duplication, but only the server can see or access the master list.

1 Like

Got it! That clears a lot up for me. I’ll go ahead and try it now. Thanks for the explanation!

So I tried setting this up with a local script, but can’t get a gui button connected via tags. I am trying this out on a blank baseplate. The structure I am trying to replace is below. This works to print the local player’s name when the button is pressed.

The new structure with CollectionService is:

When I implement this structure, everything is printed (including the last print("Connected), but the button.Activated event does not work. Nothing happens when I press the button. Is this the correct implementation of the CollectionService?

EDIT: Also the ScreenGui is tagged with the TagEditor plugin like this:
image

Well, in that case the CollectionService implementation seems correct and I don’t really see why that could be happening, but definitely check out the button and its properties or try to use other event, maybe adding the function in the same for loop?

Does not look as a CollectionService issue.

1 Like

I see, thanks! I got it to work by just adding a wait(1) at the beginning of the local script. So you’re right, no CollectionService issue.