Profileservice data returning nil

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)

For context, the saving and loading of the data works perfectly fine, but I cannot access it in this script.

you are getting the data as soon as the server script runs, which is before the player joins so no data exists for them so the get function returns nil

also small note, in the first script you are editing player gui on the server, dont do that move it to a local script

thank you very much, i’ll add a delay when i get the chance. about the server script, you can’t access profileservice data on the client (apparently)

but you can send it over through a remoteevent
or request from the client with a remote function

ah ok, thank you very much! i appreciate it

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