I’ve been trying to use knit components for a while but i didn’t manage to get it done, I’ve seen youtube videos, the documentation, you name it, I tried, I still haven’t found a solution, this is my current scripts.
Component
local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)
local Component = require(Knit.Util.Component)
local MyComponent = {}
MyComponent.__index = MyComponent
MyComponent.Tag = "MyComponent"
-- CONSTRUCTOR
function MyComponent.new(instance)
local self = setmetatable({}, MyComponent)
print("heldaps")
-- Add more properties to self before returning like self.TimeCreated = tick()
return self
end
return MyComponent
Initializer
local Knit = require(game.ReplicatedStorage.Packages.Knit)
local Component = require(Knit.Util.Component)
Knit.AddServices(script.Parent.Services)
for i, v in pairs(script.Parent.Components:GetChildren()) do
require(v)
end
Knit.Start():catch(warn)
So, you’re doing it the old way. Assuming you’re using the latest version, this is how I do it:
Component
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Component = require(ReplicatedStorage.Packages.Component)
local Trove = require(ReplicatedStorage.Packages.Trove)
local TestComponent = Component.new({ Tag = "SomeTag" })
function TestComponent:Construct()
self._Trove = Trove.new()
end
function TestComponent:Start() end
function TestComponent:Stop()
self._Trove:Clean()
end
return TestComponent
The trove part is optional of course. There are other methods too and other stuff that you can read from the documentation.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Knit = require(ReplicatedStorage.Packages.Knit)
local StartTime = tick()
local function RoundDecimalPlaces(Number: number, DecimalPlaces: number)
local Multiple = 10 ^ (DecimalPlaces or 0)
return math.floor(Number * Multiple + 0.5) / Multiple
end
local function OnStartSuccess()
warn(string.format("[SERVER]: Started (%s)", tostring(RoundDecimalPlaces(tick() - StartTime, 3))))
end
local function OnStartFailure(Error)
warn(string.format("[SERVER]: Errored (%s)", tostring(RoundDecimalPlaces(tick() - StartTime, 3))))
warn(Error)
end
for _, Dependency: ModuleScript in ipairs(script:GetDescendants()) do
local IsModuleScript = Dependency:IsA("ModuleScript")
local IsComponent = Dependency:IsDescendantOf(script.Components) and Dependency.Name:match("Component$")
local IsService = Dependency:IsDescendantOf(script.Services) and Dependency.Name:match("Service$")
if IsModuleScript and (IsComponent or IsService) then
require(Dependency)
end
end
Knit.Start():andThen(OnStartSuccess):catch(OnStartFailure):await()
This is how I do it. What you have looks like it works fine besides the fact you require component when it’s unnecessary.
Knit Game Tutorial - YouTube
This video helped me understand components and knit, may be useful if you haven’t seen it.