Help with Money Service in Knit Framework

You can write your topic however you want, but you need to answer these questions:
I wnat a service that can handle everything that has to do with my money.

For some reason it just returns nil when it is supposed to have a default of 5000 and 100 if it is nil

local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Money_Store_1858918")

local starting_bank = 5000
local starting_wallet = 100

local function checkifnew(player:Player)
	print("Ran")
	local success, data = pcall(function()
		return DataStore:GetAsync(player.UserId)
	end)

	if success then
		if data == nil then
			DataStore:SetAsync(player.UserId, {
				["Bank"] = starting_bank,
				["Wallet"] = starting_wallet
			})
		end
	else
		warn("Error fetching data for player:", player.UserId)
	end
	return true
end

local MoneyService = Knit.CreateService {
	Name = "MoneyService",
	Middleware = {
		Inbound = {checkifnew},
		Outbound = {checkifnew}
	},
	Client = {}
}

function MoneyService.Client:GetAll(player, callback)
	self.Server:GetAll(player, callback)
end

function MoneyService:GetAll(player:Player, callback)
	local success, data = pcall(function()
		return DataStore:GetAsync(player.UserId)
	end)
	
	print(player.UserId)
	if success then
		callback(data)
	else
		warn("Error fetching data for player:", player.UserId)
		callback(nil)
	end
end

function MoneyService.Client:GetWallet(player, callback)
	self.Server:GetWallet(player, callback)
end

function MoneyService:GetWallet(player:Player, callback)
	local success, data = pcall(function()
		return DataStore:GetAsync(player.UserId)
	end)

	if success and data then
		callback(data.Wallet)
	else
		warn("Error fetching wallet for player:", player.UserId)
		callback(nil)
	end
end

function MoneyService.Client:GetBank(player, callback)
	self.Server:GetBank(player, callback)
end

function MoneyService:GetBank(player:Player, callback)
	local success, data = pcall(function()
		return DataStore:GetAsync(player.UserId)
	end)

	if success and data then
		callback(data.Bank)
	else
		warn("Error fetching bank for player:", player.UserId)
		callback(nil)
	end
end

function MoneyService:GiveWallet(player:Player, amount:number)
	local wallet = self:GetWallet(player, function(wallet)
		local bank = self:GetBank(player, function(bank)
			if wallet and bank then
				DataStore:SetAsync(player.UserId, {
					["Bank"] = bank,
					["Wallet"] = wallet + amount
				})
			end
		end)
	end)
end

function MoneyService:GiveBank(player:Player, amount:number)
	local wallet = self:GetWallet(player, function(wallet)
		local bank = self:GetBank(player, function(bank)
			if wallet and bank then
				DataStore:SetAsync(player.UserId, {
					["Bank"] = bank + amount,
					["Wallet"] = wallet
				})
			end
		end)
	end)
end

function MoneyService:RemoveWallet(player:Player, amount:number)
	local wallet = self:GetWallet(player, function(wallet)
		local bank = self:GetBank(player, function(bank)
			if wallet and bank then
				DataStore:SetAsync(player.UserId, {
					["Bank"] = bank,
					["Wallet"] = wallet - amount
				})
			end
		end)
	end)
end

function MoneyService:RemoveBank(player:Player, amount:number)
	local wallet = self:GetWallet(player, function(wallet)
		local bank = self:GetBank(player, function(bank)
			if wallet and bank then
				DataStore:SetAsync(player.UserId, {
					["Bank"] = bank - amount,
					["Wallet"] = wallet
				})
			end
		end)
	end)
end

function MoneyService:KnitInit()
	game:GetService("Players").PlayerAdded:Connect(function(player)
		self:GetAll(player, function(data)
			print(data)
		end)
	end)
end

return MoneyService

I recommend stop using Knit, it is being archived soon

Very often when saving in studio while editing, the data sometimes will be saved to ‘{}’ or ‘nil’.

I used to spent hours trying to figure out why my data store is nil when it should have data, till I realized that I saved the data store in a period where the data was actually nil due to an error in the PlayerRemoving event, so whenever I used GetAsync, the ‘nil’ or ‘{}’ I was getting, was indeed the data I had saved.

Long story short: just wipe your current data and make sure something is saved, if you still get ‘nil’ it’s a problem with how you get or set the data.

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