Skip to main content

Components

Components are the building blocks of your game. They are reusable code blocks that can be used to build your game. Components can be anything that you can attach a tag to, such as a BasePart, a Model, a Script, etc. Components core objective is to make your code more modular and reusable.

There a few different types of components that you can create:

  • Streamed Components
  • Vicinity Components
  • Tool Components

Streamed Components

Streamed Components are components that watch for an object that has been streamed in and out. This means that you can easily attach functionality to and Instance that could potentially not exist. This is useful for things that are not always in the game, such as other player's character.

tip

Streamed Components can be used both on the client and the server. However, on the server this will always be availaable as the server allways has all the instances streamed in.

Vicinity Components

Vicinity Components are components that watch for an object that is within a certain distance of the player. This means that you can easily attach functionality to an Instance that is within a certain distance of the player. This is useful for when you want to attach functionality to an instance only when the player is within a certain distance of it of your choosing.

caution

Vicinity Components can only be used on the client.

Tool Components

Tool Components are components that are attached to a tool. This means you no longer have to store your tool's functionality in a script inside the tool. This is useful when you want to have multiple tools that have the same functionality, but you don't want to have to copy and paste the same code into each tool.

caution

Tool Components can only be used on the client.

Creating a Component

To create a component, you must first create a new ModuleScript inside of the components folder. This ModuleScript will be the component's code. You can name this ModuleScript whatever you want, but it is recommended that you name it to the tag name that you will be using for the component.

Once you have created the ModuleScript, you can start writing your component's code. The component's code will be run when the component is attached to an Instance. This means that you can use the Instance variable to access the Instance that the component is attached to.

local Cradle = require(game:GetService("ReplicatedStorage").Cradle)

local Example = {}
Example.__index = Example
Example.Name = script.Name

function Example.new(instance)
local self = {}
self._inst = instance
return setmetatable(self, Example)
end

function Example:Init()
print("Part tagged with Example has been added")
print(self._inst)
end

function Example:Destroy()
print("Part tagged with Example has been removed")
print(self._inst)
end

local ExampleComponent = Cradle.Component.new(Example, "streamed", {})
return ExampleComponent
tip

See the Boilerplates guide to get more boilerplates for components.