I want to learn about CollectionService and RunService

Hello, I got few questions about scripting, So, how does collectionservice works, and if possible give me some examples for I could learn!! And about RunService, I really can’t understand what’s the function so!! please help me, I want to learn, also if possible give me some examples!

CollectionService is a service which helps you manipulate instances more easily by using tags.
For example, you want to make many bricks which kills people when they touches.
You can use CollectionService to reduce the number of scripts (which is a good thing)
Before that, I recommend you to install the plugin below so you can manage instances more easily.

https://www.roblox.com/library/948084095

Here’s an example of how to use CollectionService on killing bricks:

local Collection = game:GetService("CollectionService") -- Gets the service
local list = Collection:GetTagged("Test") -- Fill in the tag name you created
--[[
The list above returns an array,
we can iterate the list by using a for loop
]]
for i,v in pairs(list) do
    print("The name of the instance is: "..v.Name)
end

RunService is a service which provides methods on managing time. It contains properties such as IsStudio() and IsClient().
The most commonly used method in RunService is BindToRenderStep() and UnbindFromRenderStep()
BindToRenderStep() is a event that fires every frame, in which connects to a function, similar to :Connect().
RunService:BindToRenderStep() takes 3 parameters, the name, priority and function.

  • name is a string which may be used to disconnect by other methods.
  • priority is an int which is used by Roblox to arrange the functions, which 1 gets the highest priority.
  • function is a function which is used to place your custom codes to be fired every frame

RunService:UnbindFromRenderStep() is a method to disconnect functions connected by RunService:BindToRenderStep().
UnbindFromRenderStep() takes 1 parameter, which is the string defined above.
Here’s a use of the RunService:

local RS = game:GetService("RunService") -- Gets the RunService
local count = 0 -- to count the times
RS:BindToRenderStep("Test",200,function()
    print("This sentence is printed by BindToRenderStep!") -- print messages
    count = count + 1 -- increase the count
    if count == 10 then -- to detect whether 10 frames had passed
        RS:UnbindFromRenderStep("Test") -- If yes, unbind the function
    end
end)

I hope you know how to use CollectionService and RunService! :grinning:

2 Likes

There are tons of great tutorials on the devforums, on YouTube, and on the devhub, so be sure to check out those resources before making a thread!

Here’s a pretty helpful tutorial by AlvinBlox for CollectionService:


And if you want to see all of the functions CollectionService has and how you can use them, the devhub has a whole list of them:https://developer.roblox.com/en-us/api-reference/class/CollectionService

RunService serves a few different purposes, including BindToRenderStep, which is used for constantly executing a portion of code. There’s also a variety of functions used to detect which side of the network the script is running on, whether or not it’s in Studio, and so on.

Are you just asking for code samples or did you genuinely try understanding it yourself first? Not the point of this category to ask for code. Please search first before posting; you can find most of this information directly from the API pages.

I did yes, I asked for example, not for codes as in wiki I couldn’t find anything very clear as my language is spanish too!

Thank you so much!! :grinning::grinning::grinning::grinning::grinning:

I have tag editor, but I don’t know how to use it, can you teach me? Or, just tell me how to put tags on something. I know this isn’t my topic, but I really need to deduct some of my scripts, I have way too much(more than 100 lol) and most of them do the same things. Thank you :smiley:

There’s a DevForum thread right on the description of the Tag Editor plugin that contains information about CollectionService, the relevance of the tag editor plugin and how to use it.

Helps to be vigilant and search for these kinds of things. A lot of these simple questions only take a quick search around your environment first and foremost before sending to the forums for help.

1 Like