Knit Client Server Communication Help

I’m making a coin service with knit and I’m having trouble with the client communicating with the server.

Service (Server):

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

local CoinService = Knit.CreateService {
	Data = {},
	Name = "CoinService";
    Client = {};
}

function CoinService:Get(player: Player)
	return CoinService.Data[player]
end

function CoinService:Add(player: Player, Amount: number)
	CoinService.Data[player] += tonumber(Amount) or 0
end

function CoinService:Remove(player: Player, Amount: number)
	CoinService.Data[player] -= tonumber(Amount) or 0
end

function CoinService.Client:GetCoins(player: Player)
	return CoinService.Data[player]
end

function CoinService:KnitStart()
	for i, v in pairs(Players:GetPlayers()) do
		CoinService.Data[v] = math.random(1, 500)
	end
	Players.PlayerAdded:Connect(function(player)
		CoinService.Data[player] = math.random(1, 500)
	end)
	Players.PlayerRemoving:Connect(function(player)
		CoinService.Data[player] = nil
	end)
end

function CoinService:KnitInit()
	
end

return CoinService

Controller(Client):

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

local Coins = Knit.CreateController {
    Name = "Coins";
}

function Coins:KnitStart()
	local CoinService = Knit.GetService("CoinService")
	
	local PlayerCoins = CoinService:GetCoins()
	
	print(PlayerCoins)
end

function Coins:KnitInit()
	
end

return Coins

It doesn’t print the players coins, it prints “Promise(Started)”

I tried printing the players coins in the CoinService.Client:GetCoins() function and it works, but it doesn’t send the value to the client?
image

I don’t think you can share values between server and client via module script. Last time I did this with a cooldown module, it printed out a bunch of data. I would try storing coins in an instance or attribute instead.

Knit allows you too, I was following along his video tutorial and came across this error.

you definitely can.

@sinsbygod as for your Promise(Started) issue, you need to disable service promises. To do that in both the server and client where you call knit.Start(), you need to pass the following:

knit.Start({ServicePromises = false})

this is because by default knit will return promises

1 Like

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