Can you use Knit services in other Knit Services?

Hey okay so basically I’m currently making a game using the Knit game framework, and I’m having some issues with using services in other services, for example:

Service 1:

local replicatedStorage = game:GetService("ReplicatedStorage")
local serverScriptService = game:GetService("ServerScriptService")
local serverStorage = game:GetService("ServerStorage")
local players = game:GetService("Players")

local packages = replicatedStorage:FindFirstChild("Packages")

local knit = require(packages:FindFirstChild("Knit"))
local profileService = require(serverStorage:FindFirstChild("ProfileService"))

local dataService = knit.CreateService {
	Name = "DataService",
	Client = {},
	_profiles = {}
}

Service 2:

local replicatedStorage = game:GetService("ReplicatedStorage")
local serverScriptService = game:GetService("ServerScriptService")
local serverStorage = game:GetService("ServerStorage")
local players = game:GetService("Players")

local packages = replicatedStorage:FindFirstChild("Packages")
local knit = require(packages:FindFirstChild("Knit"))

!! local dataService = knit.GetService("DataService")

local dataController = knit.CreateService {
	Name = "DataController",
	Client = {
		killsChanged = knit.CreateSignal();
		winsChanged = knit.CreateSignal();
		balanceChanged = knit.CreateSignal();
	},
}

Service 2 always fails to load, errors on the line prefixed with "!! ".
The error is that you can’t get a service until Knit has started, however Knit can’t start until all of the Services are loaded. It’s like a loop :weary:

I know it must be possible to do this, just don’t know how.
Thanks

The issue here is that you’re using Knit wrong. The idea of Knit is to have a balanced life cycle as seen:

Meaning that services are called to start using Service:KnitInit(), then once all services finish that cycle, they all move to Service:KnitStart(). This avoids race conditions.

So in your case, the fix would be something along the lines of:

local replicatedStorage = game:GetService("ReplicatedStorage")
local serverScriptService = game:GetService("ServerScriptService")
local serverStorage = game:GetService("ServerStorage")
local players = game:GetService("Players")

local packages = replicatedStorage:FindFirstChild("Packages")
local knit = require(packages:FindFirstChild("Knit"))

local dataService

local dataController = knit.CreateService {
	Name = "DataController",
	Client = {
		killsChanged = knit.CreateSignal();
		winsChanged = knit.CreateSignal();
		balanceChanged = knit.CreateSignal();
	},
}

function dataController:KnitStart() // access services
	dataService:Something()
end

function dataController:KnitInit() // setup services
	dataService = knit.GetService("DataService")
end
1 Like

Hm okay thank you!

This is my first time ever using knit so apologies if it’s a bit of a stupid mistake