ProfileService data storage issue with playerAdded

I am using the ProfileService module for this.

I use a function to get a players profile but it only works after the data/module has been initialized with a seperate function. (Init()). I am trying to update a leaderstat using the profile data through a playerAdded function but it fires before the profile data is loaded (even though the initialization function is above it.) any suggestions on how I could fix this? I’ll send all relevant scripts.

If you need anymore information, or scripts, or need a better explanation, please ask!

Server script causing the problem

local ProfileService = require(game.ServerScriptService.ProfileService)
local PlayerDataHandler = require(game.ServerScriptService:WaitForChild("PlayerDataHandler"))
local Players = game:GetService("Players")


PlayerDataHandler:Init()
wait(1)  -- this makes or breaks the script, saying that It cant find a profile for the player's user ID. (the player added function fires before the profiles load)
Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	local Money = Instance.new("IntValue", leaderstats)
	
	
	leaderstats.Name = "leaderstats"
	Money.Name = "Money"
	
	Money.Value = PlayerDataHandler:Get(player, "Money")
	
	leaderstats.Parent = player
end)

Modular function that gives the error

 local function getProfile(player)
	assert(Profiles[player], string.format("Cannot find profile for %s", player.UserId))
	
	return Profiles[player]
end

and the modular function that calls for the other one (to get data):

function PlayerDataHandler:Get(player, key)
	local profile = getProfile(player)
	assert(profile.Data[key], string.format("There is no data with the key: %s", key))
	
	return profile.Data[key]
end

It would help if you showed the init function because I don’t see much reason why your player added function wouldn’t be able to work because the only prerequisites are the profile manager, and the ProfileStore to exist.

1 Like
function PlayerDataHandler:Init()
	for i, player in game.Players:GetPlayers() do
		task.spawn(playerAdded, player)
	end

is the INIT function, I figured out if I combine the playerAdded functions together it seems to work mostly correct.

Why are you doing this?

just using Players.PlayerAdded would be better with getting the players in your game.

If your PlayerDataHandle:Init() function actually gets the data from ProfileService try modifying your function so it initializes it for each player within the Players.PlayerAdded signal.

Yeah you have to wait for the profiles to load in after the players join the server.

I would just recommend adding the PlayerAdded Event with the Init function after doing the for i loop, so then you have all players that are already in game covered and that it will catch any new players after.

Example of what i’m trynna explain:

local function Setup()
-- Init Function
-- PlayerAdded Event
end

I added what I wanted to happen into the Init function and it ended up working just fine

That’s great to hear glad everything worked out.

1 Like

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