Skip to main content

Boilerplates

--[[

Title: Example
Version: 0.1.0

+
| Author: OliverStrutt
| Date: DD/MM/YYY
| Description:
|
| API:
|
| Requirements:
| -
+

]]

Service

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

local ExampleService = Cradle:CreateService({
Name = "ExampleService",
Client = {
someEvent = Cradle.RemoteComm.RemoteEvent.new(),
someTableProperty = Cradle.RemoteComm.RemoteProperty.new({}),
someNumberProperty = Cradle.RemoteComm.RemoteProperty.new(123),
},
})

local ExampleModule = Cradle.Modules.ExampleModule or Cradle.Shared.Modules.ExampleModule

function ExampleService.Client:ServerFunction()
print("This was called from the client as a RemoteFunction")
end

function ExampleService:ExampleMethod()
print("This was called from a method")
end

function ExampleService:Start()
self.Client.someEvent:Connect(function()
print("This was fired from the client as a RemoteEvent")
end)
ExampleModule:someFunction()
end

function ExampleService:Init()
game.Players.PlayerAdded:Connect(function(player)
self.Client.someEvent:FireClient(player)
end)
end

return ExampleService
tip

Check out the Services guide to learn more about them.

Controllers

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

local ExampleModule = Cradle.Modules.ExampleModule or Cradle.Shared.Modules.ExampleModule

local ExampleController = Cradle:CreateController({
Name = "ExampleController",
})

function ExampleService:ExampleMethod()
print("This was called from a method")
end

function ExampleController:Start()
local ExampleService = Cradle:GetService("ExampleService")

ExampleService:ServerFunction()
ExampleService.someEvent:Connect(function()
print("The server has fired an event")
end)
ExampleService.someEvent:FireServer()

ExampleModule:someFunction()
end

function ExampleController:Init() end

return ExampleController
tip

Check out the Controllers guide to learn more about them.

Component

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

local Example = {}
Example.__index = Example
Example.Name = script.Name -- Recommended to be the same as the tag 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 destroyed")
print(self._inst)
end

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

Tool Component

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

local Example = {}
Example.__index = Example
Example.Name = script.Name -- Recommended to be the same as the tag name

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

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

function Example:Deiniit()
print("Tool tagged with Example has been unequipped")
print(self._inst)
end

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

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

Vicinty Component

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

local Example = {}
Example.__index = Example
Example.Name = script.Name -- Recommended to be the same as the tag 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:Deinit()
print("Part tagged with Example has been removed")
print(self._inst)
end

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

local ExampleComponent = Cradle.Component.new(Example, "streamed", {
Distance = 5, -- The distance the player has to be within to trigger the component
})
return ExampleComponent
tip

Check out the Components guide to learn more about them.