Hey! I’m having a problem in knit and not sure how to really approach this. So let me break it down as best I can. So I am using ProfileService to save/manage data and with that, it should be relatively easy to send data to the client/server. Now for the past day, I have been trying to figure out how the client and server can access the data. So let me show you the scripts and then explain more on what I have done.
Server Script (DataService):
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
-- Modules
local Knit = require(ReplicatedStorage:WaitForChild("Common"):WaitForChild("Knit"))
local UserData = require(script.Parent.Parent:WaitForChild("Module"):WaitForChild("DataTable"))
local ProfileService = require(ReplicatedStorage:WaitForChild("Common"):WaitForChild("ProfileService"))
local DataService = Knit.CreateService{
Name = "DataService",
Client = {},
}
local ProfileStore = ProfileService.GetProfileStore(
"PlayerDataTest",
UserData
)
local Profiles = {}
function DataService:GetData(player)
if not player then return end
print(Profiles)
return Profiles[player].Data
end
function CreateProfile(player)
local profile = ProfileStore:LoadProfileAsync("Player_"..player.UserId)
if profile then
profile:Reconcile() -- Fills missing variables from UserData
profile:ListenToRelease(function() -- If profile is loaded in another Roblox Server
Profiles[player] = nil
player:Kick()
end)
if player:IsDescendantOf(Players) then
Profiles[player] = profile -- Profile Loaded
else
profile:Release() -- Player Left before their profile loaded
end
else
player:Kick() -- Another server was loading the profile at the same time
end
end
function RemoveProfile(player)
local profile = Profiles[player]
if profile then
profile:Release()
end
end
function DataService.Client:GetPetData(player)
if player then
return self.Server:GetData(player).Inventory.Pets
end
end
function DataService:KnitInit()
-- Player Events
Players.PlayerAdded:Connect(CreateProfile)
Players.PlayerRemoving:Connect(RemoveProfile)
-- Safety
for _, player in ipairs(Players:GetPlayers()) do -- Just incase player spawns before script runs
task.spawn(CreateProfile, player)
end
game:BindToClose(function() -- If game closes/ Shuts down
for _, player in ipairs(Players:GetPlayers()) do
task.spawn(CreateProfile, player)
end
end)
end
function DataService:KnitStart()
end
return DataService
So this is my DataService script. I have a function called DataService:GetData()
which you can see and whenever I call this from another server script it prints out nothing now I am calling this from another server script called WeaponService and I am running this in the :KnitInit()
function:
-- Function
function WeaponService:GetCurrentWeapon(player)
return Knit.Services.DataService:GetData(player)
end
function WeaponService:KnitInit()
Players.PlayerAdded:Connect(function(player)
print(self:GetCurrentWeapon(player))
end)
end
What I’m thinking is the player data doesn’t exist at the time I call it which could be the case, if it is what would be the fix to this?
Now this is just data from Server to Server but when I try to get data from the client to server it goes horribly wrong. Also is it bad for the client to retrieve data instead of the server sending the data?
My client setup, so I was trying to make a pet inventory which is why in my DataService script there is a function called DataService.Client:GetPetData(player)
so I could wait for the client to ask the server for data. Now in my head this should work but it doesn’t.
PetInventory Client:
local Knit = require(game:GetService("ReplicatedStorage"):WaitForChild("Common").Knit)
local Janitor = require(Knit.Util.Janitor)
local DataService = Knit.GetService("DataService")
local PetInventory = {}
PetInventory.__index = PetInventory
PetInventory.Tag = "PetInventory"
function PetInventory:SetUp()
end
function PetInventory:Refresh()
end
function PetInventory.new(guiManager, playerGui)
local self = setmetatable({}, PetInventory)
self._janitor = Janitor.new()
guiManager._GetAsset("Weapons"):Clone().Parent = playerGui:WaitForChild("Main")
-- Need to get pet Data
print(DataService:GetPetData(guiManager._Player))
self:SetUp()
return self
end
function PetInventory:Init()
end
function PetInventory:Deinit()
end
function PetInventory:Destroy()
self._janitor:Destroy()
end
return PetInventory
As you can see I call it but I still believe the issue is, whilst writing this I believe I realised the only real issue is the data isn’t loading fast enough so there is no Data when the scripts request it.
How could I make it so the data is loaded before any other scripts request it? This is just so I can request it in any script and there shouldn’t be any issues.
TL:DR
How would I fix the data not loading in time?
My scripts work but there is no player data so the output shows an empty table {}
Edit: Just looking through the API should I use Options in this scenario?
Sorry for the long post most things may be unnecessary but I thought I would give as much detail as possible as to why I did what I did. I apologize in advanced and appreciate any help given