Cannot call GetController until Knit has been started[Knit Error]

im a newbie at knit and there isn’t very much info on it online. But i’m trying to make a simple controller but anytime I called .GetController it’ll error.

My controller code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local packages = ReplicatedStorage:WaitForChild("Packages")

local knit = require(packages:WaitForChild("Knit"))
local signal = require(packages:WaitForChild("Signal"))

local cameraController = knit.CreateController {
    Name = "CameraController"
}

cameraController.Distance = 20
cameraController.Locked = false

function cameraController:Lock(part)
    local camera = workspace.CurrentCamera

    camera.CameraType = Enum.CameraType.Scriptable
    camera.CFrame = part.CFrame * CFrame.new(0,0,self.Distance)
end

function cameraController:Unlock()
    local camera = workspace.CurrentCamera

    camera.CameraType = Enum.CameraType.Custom 
end

knit.Start()

return cameraController

my client code

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local knit = require(ReplicatedStorage:WaitForChild("Packages"):WaitForChild("Knit"))

local CameraController = knit.GetController("CameraController") -- errors here

local newPart = Instance.new("Part")

newPart.Anchored = true
newPart.Name = "Part"
newPart.Parent = workspace

CameraController:Lock(newPart)

any help is appreciated thanks!

1 Like

I’m not exactly sure what is causing the issue but you shouldn’t have knit.Start() in your controller. The recommended way is to have a Init-file for the client and one for server that will Start Knit and makes sure everything is loaded in order.

More documentation can be found here:
https://sleitnick.github.io/Knit/docs/gettingstarted

1 Like

it removes the error but now it has a different one “Could not find controller “CameraController”. Check to verify a controller with this name exists”

my controller:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local packages = ReplicatedStorage:WaitForChild("Packages")

local knit = require(packages:WaitForChild("Knit"))
local signal = require(packages:WaitForChild("Signal"))

local cameraController = knit.CreateController{ Name = "CameraController" }


cameraController.Distance = 20
cameraController.Locked = false

function cameraController:Lock(part)
    local camera = workspace.CurrentCamera

    camera.CameraType = Enum.CameraType.Scriptable
    camera.CFrame = part.CFrame * CFrame.new(0,0,self.Distance)
end

function cameraController:Unlock()
    local camera = workspace.CurrentCamera

    camera.CameraType = Enum.CameraType.Custom 
end

return cameraController

my client:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local knit = require(ReplicatedStorage:WaitForChild("Packages"):WaitForChild("Knit"))

knit.Start():andThen(function()
    local controller = knit.GetController("CameraController") -- warns here
end):catch(warn)

thx for ur help btw

1 Like

This is how I set things up:

Server Init in ServerStorage:

local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)

Knit.AddServicesDeep(script.Parent.Services)
-- Make sure to have Services folder in ServerStorage

Knit.Start({ServicePromises = false}):catch(warn)

Client Init in StarterPlayerScripts:

local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)

Knit.AddControllersDeep(script.Parent:WaitForChild('Controllers'))
-- Make sure to have Controllers folder and all your controllers here

Knit.Start({ServicePromises = false}):catch(warn)

Here is a controller example in StarterPlayerScripts

local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)
local TestController= Knit.CreateController { Name = "TestController" }

function TestController:KnitInit()
 print ("initiating controller stuff")
end

function TestController:KnitStart()
print ("doing things in controller after knit started")
end

function TestController:SomeOtherFunction()
print ("Doing stuff")
end

return TestController

If you follow this pattern of initiating Knit and settings up controllers then I think you should be able to make it work.

2 Likes

ty it worked now. Turns out u have to do .AddControllersDeep.

Didnt know that thx!

Glad it worked.
The Deep thing means that it also loads controllers in subfolders and not just one layer deep. So you don’t really need to use deep to make it work if you don’t want to. It depends on your file structure.

1 Like