-
What do you want to achieve?
Successfully fetch my data table for any player through Profile Service. -
What is the issue?
My Profile Template reads
{
Credits = 0,
Inventory = {},
XP = 0,
Stats = {
Wins = 0, Losses = 0
},
Achievements = {}
}
And when fetching the Profile for a player, the profile is found, but anything inside that data is nil.
For example in the above code, The table is fetched, but the Credits value in the table is always nil.
Here’s my DataService and code in another script I’m using to test the DataService.
DataService
local RepS = game.ReplicatedStorage
local Pack = RepS.Packages
local Knit = require(Pack.Knit)
local Modules = game.ReplicatedStorage.Modules
local ProfileService = require(Modules.ProfileService)
local DataService = Knit.CreateService
{
Name = "DataService",
Client = {
InfoRequest = Knit.CreateSignal()
},
}
--// Module Values
local Profiles = {}
local ProfileStore = ProfileService.GetProfileStore (
"Player",
{
Credits = 0,
Inventory = {},
XP = 0,
Stats = {
Wins = 0, Losses = 0
},
Achievements = {}
}
)
--// Support Functions
local function onPlayerAdded(plr)
local profile = ProfileStore:LoadProfileAsync(
"plr_"..plr.UserId,
"ForceLoad"
)
if profile then
profile:Reconcile()
profile:ListenToRelease(function()
Profiles[plr] = nil
plr:Kick()
end)
if plr then
Profiles[plr] = profile
print("Player","("..plr.Name..")", "has joined the game with Creds:",Profiles.Credits)
else
profile:Release()
end
else
plr:Kick()
end
end
local function onPlayerRemoved(plr)
local profile = Profiles[plr]
if profile then
profile:Release()
Profiles[plr] = nil
end
end
--// Module Functions
function DataService:KnitInit()
game.Players.PlayerAdded:Connect(onPlayerAdded)
game.Players.PlayerRemoving:Connect(onPlayerRemoved)
end
function DataService:Get(plr)
local profile = Profiles[plr]
if profile then
return profile
end
end
return DataService
Test Script
DataService = Knit.GetService("DataService")
while task.wait(10) do
for _,plr in ipairs(game.Players:GetChildren()) do
local profile = DataService:Get(plr)
if profile then
if profile.Credits == nil then
profile.Credits = 0
end
profile.Credits += 10
print("Added 10 Creds to Player","("..plr.Name..")","for joining the game")
else
print("Profile of Player","("..plr.Name..")","does not exist yet")
end
end
end
- What solutions have you tried so far?
I read some posts which had a similar title. One issue got fixed because they forgot to return their module, which I have done. Another was suggested to use DataService:Reconcile(), which I have done. Another just didn’t have a solution marked.