Easy Written By howmanysmaII

CollectionService Tutorial Created on: 18-10-2019

This tutorial covers how to use CollectionService.

Welcome to a tutorial on how to use CollectionService, quite possibly the most useful service. We're gonna need an external plugin for this. Let's start by installing it.

First up, open Studio and go into the plugin management. We're going to search for the plugin called "Tag Editor" by Sweetheartichoke. This will allow us to tag any instances with a click of a button.

Now that you've installed it, I'll show you how to use it. In a place, open the Tag Editor plugin, and a window that looks like this should open up.



Let's make a simple obby using it. First up, click on the TextBox that says "Add new tag...". Here, you should type in "Lava" (without quotes, of course). It should look like this:



Now, create a part in the Workspace. You can style it however you want to, I'll just make it red and have the granite material, don't forget to anchor it as well. Once you have that done, click on it and then click the plugin button that says Lava. This will give the part the CollectionService tag Lava. You can go ahead and duplicate the lava part. It should look like this:





Now, we're gonna script the Lava to work. Insert a script into ServerScriptService, call it Main or something. We're gonna start this script by declaring CollectionService.

local CollectionService = game:GetService("CollectionService")

Now, we're going to make a killing function.

local CollectionService = game:GetService("CollectionService")

local function KillOnTouch(Hit)
end

In this function, we want to find the Character and Humanoid of whatever touched the Lava part. To do this, we're going to use ::FindFirstAncestorOfClass and ::FindFirstChildOfClass.

local CollectionService = game:GetService("CollectionService")

local function KillOnTouch(Hit)
	local Character = Hit:FindFirstAncestorOfClass("Model")
	if Character then
		local Humanoid = Character:FindFirstChildOfClass("Humanoid")
		if Humanoid then
			Character:BreakJoints()
		end
	end
end

This code will check for a model ancestor, and if it finds one, it'll search for a Humanoid in it. If it finds the Humanoid, it should break the joints of the model. Now, we want to implement the Actual CollectionService work. We'll be using the function CollectionService::GetTagged, which returns an array of every tagged Instance. We want to iterate over this using ipairs in the new VM, since it is the fastest way of doing it.

local CollectionService = game:GetService("CollectionService")

local function KillOnTouch(Hit)
	local Character = Hit:FindFirstAncestorOfClass("Model")
	if Character then
		local Humanoid = Character:FindFirstChildOfClass("Humanoid")
		if Humanoid then
			Character:BreakJoints()
		end
	end
end

for _, LavaPart in ipairs(CollectionService:GetTagged("Lava")) do
end

Since being safe is always a good idea, we're going to check to make sure LavaPart is actually a BasePart, so that way if a non-BasePart is tagged, it won't error and prevent the code from running.

local CollectionService = game:GetService("CollectionService")

local function KillOnTouch(Hit)
	local Character = Hit:FindFirstAncestorOfClass("Model")
	if Character then
		local Humanoid = Character:FindFirstChildOfClass("Humanoid")
		if Humanoid then
			Character:BreakJoints()
		end
	end
end

for _, LavaPart in ipairs(CollectionService:GetTagged("Lava")) do
	if LavaPart:IsA("BasePart") then
	end
end

Now, we want to hook up the .Touched connection to the LavaPart.

local CollectionService = game:GetService("CollectionService")

local function KillOnTouch(Hit)
	local Character = Hit:FindFirstAncestorOfClass("Model")
	if Character then
		local Humanoid = Character:FindFirstChildOfClass("Humanoid")
		if Humanoid then
			Character:BreakJoints()
		end
	end
end

for _, LavaPart in ipairs(CollectionService:GetTagged("Lava")) do
	if LavaPart:IsA("BasePart") then
		LavaPart.Touched:Connect(KillOnTouch)
	end
end

And just like that, your script will make all LavaParts kill on touch, and it works!



You can do so many things easier with CollectionService opposed to using Folders and whatnot. This is a very, very, very useful service, and it makes Roblox development a bit easier.

Easy Written By howmanysmaII

See howmanysmaII's profile on Roblox

Discussion