Creating remote events in components using knit

Hello! (Using mobile so correct me by any means) I’ve started to use the “Knit” method of Roblox studio programming but I wanted to know if I could create remote events in components as I kinda find it a hassle to create a whole new service for one single remote

Yes, you can create RemoteEvents within components when using the Knit method. First of all you need to create a new component (create the folder with a script inside).
Inside the component script, you can create a RemoteEvent:

-- MyComponent.lua
local Knit = require(game:GetService("ReplicatedStorage").Knit)

local MyComponent = Knit.CreateComponent {
    Name = "MyComponent";
}

-- Creating the RemoteEvent
local remoteEvent = Instance.new("RemoteEvent")
remoteEvent.Name = "MyRemoteEvent"
remoteEvent.Parent = game:GetService("ReplicatedStorage") -- or any other parent you prefer

function MyComponent:DoSomething(player)
    -- Implement your RemoteEvent logic here
    -- For example:
    remoteEvent:FireClient(player, "Hello from the server!")
end

return MyComponent

In the main server script (or wherever you initialize Knit), make sure to require the component:

-- MainServerScript.lua
local Knit = require(game:GetService("ReplicatedStorage").Knit)

-- Require the component
require(game.ServerScriptService.KnitComponents.MyComponent)

-- Initialize Knit
Knit.Start()

With this setup, you have a RemoteEvent created inside your component, and you can access it from anywhere in your codebase using the appropriate service or components.

The Knit framework encourages organization and modularity, so creating components for different functionalities, including RemoteEvents, helps keep your codebase clean and manageable.

Sorry for the late reply, thanks!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.