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