Hello, I am trying to use ProfileService to save player data in my game, but I have run into an issue, when I try to require my data manager, the data inside of it is nil. How can i fix this?
My data manager (ModuleScript):
local profileService = require(game.ReplicatedStorage.Modules.ProfileService)
local players = game:GetService("Players")
local profileStore = profileService.GetProfileStore(
"Player",
{
cherries = 0;
exp = 0;
maxExp = 10;
level = 1;
hasFruit = false;
fruit = "None";
fightingStyle = "Fists";
weapon = "None";
}
)
local profiles = {}
local function onPlayerAdded(player)
local profile = profileStore:LoadProfileAsync(
"Player_" .. player.UserId,
"ForceLoad"
)
if profile then
profile:Reconcile()
profile:ListenToRelease(function()
profiles[player] = nil
player:Kick("An error occured while loading your data.")
end)
if player:IsDescendantOf(players) then
profiles[player] = profile
print(profiles[player].Data.maxExp)
-- Load In Data
player.PlayerGui.LevelUI.Container.EXP.Text = "Level: ".. profile.Data.level.. " - ".. profile.Data.exp.. "/"..profile.Data.maxExp.." EXP"
player.PlayerGui.LevelUI.Container.LevelBar.Size = UDim2.new(profile.Data.maxExp / profile.Data.exp + 0.01, 1)
else
profile:AddUserId(player.UserId)
profile:Release()
print("Profile Released")
end
else
player:Kick("An error occured while loading your data, it is possible that a roblox sevice is down. Check status.roblox.com for further information.")
end
end
local function onPlayerRemoving(player)
local profile = profiles[player]
if profile then
profile:Release()
end
end
for _, player in ipairs(players:GetPlayers()) do
task.spawn(onPlayerAdded, player)
end
players.PlayerAdded:Connect(onPlayerAdded)
players.PlayerRemoving:Connect(onPlayerRemoving)
local dataManager = {}
function dataManager:Get(player)
local profile = profiles[player]
if profile then
return profile.Data
end
end
return dataManager
The script I’m trying to get the player’s data in:
local dataManager = require(game.ServerScriptService.DataManager)
local player = script.Parent.Parent.Parent.Parent
local char = player.Character
local data = dataManager:Get(player)
local replicatedStorage = game:GetService("ReplicatedStorage")
local remotes = replicatedStorage.Remotes
local function editEXP(plr, expToAdd)
if plr ~= player then return end
print(data)
print(data.exp)
end
remotes.EditStats.OnServerEvent:Connect(editEXP)