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