I am trying to update statistics from the server to client but I have an issue where when I try to update it with PlayerAdded, it doesn’t update because for some reason it thinks that there isn’t any players. But when I update after the round ends, it updates normally. How do I fix this?
local function updateData(player: Player?)
for _, player in pairs(Players:GetPlayers()) do
local playerData = Data:GetData(player)
PlayerData[player.Name] = playerData
end
if not player then
UpdateStats:Fire(BridgeNet2.AllPlayers(), PlayerData)
else
UpdateStats:Fire(BridgeNet2.Players({player}), PlayerData)
end
end
Players.PlayerAdded:Connect(function(player: Player)
updateData(player)
end)
local function updateData(player: Player?)
for _, p in pairs(Players:GetPlayers()) do
local playerData = Data:GetData(p)
PlayerData[p.Name] = playerData
end
if not player then
UpdateStats:Fire(BridgeNet2.AllPlayers(), PlayerData)
else
UpdateStats:Fire(BridgeNet2.Players({player}), PlayerData)
end
end
local function updateData(player: Player?)
for _, p in pairs(Players:GetPlayers()) do
print(p)
local playerData = Data:GetData(p)
PlayerData[p.Name] = playerData
end
print(PlayerData)
if not player then
UpdateStats:Fire(BridgeNet2.AllPlayers(), PlayerData)
else
UpdateStats:Fire(BridgeNet2.Players({player}), PlayerData)
end
end
first breakpoint printed my player name, second breakpoint (playerdata) prints “{}”.
i updated it a little to make it more readable:
local function updateData()
for _, p in pairs(Players:GetPlayers()) do
print(p)
local playerData = Data:GetData(p)
if PlayerData[p.Name] then
continue
end
PlayerData[p.Name] = playerData
end
if #PlayerData ~= 0 then
print("a")
UpdateStats:Fire(BridgeNet2.AllPlayers(), PlayerData)
end
end
Players.PlayerAdded:Connect(function(player: Player)
updateData()
end)
Also, just a little quick note, if you are filling a table using ‘keys’ such as PlayerData[p.Name] (p.Name being the key), then you can’t rely on the ‘#’ operator giving you an accurate count of the number of items in a table. It is only for indexed tables.
Double checked it and yes, i’m getting nil from Data:GetData(p). I’m using profilestore and now I think it’s because my data module is using playeradded to load all the players data.
function DataManager.onPlayerAdded(player)
local profile = ProfileStore:LoadProfileAsync(
"Player_"..player.UserId,
"ForceLoad"
)
if profile then
profile:AddUserId(player.UserId)
profile:Reconcile()
profile:ListenToRelease(function()
Profiles[player] = nil
player:Kick()
end)
if player:IsDescendantOf(Players) then
Profiles[player] = profile
local globalUpdates = profile.GlobalUpdates
for index, update in pairs(globalUpdates:GetActiveUpdates())do
globalUpdates:LockActiveUpdate(update[1])
end
for index, update in pairs(globalUpdates:GetLockedUpdates())do
HandleLockedUpdate(globalUpdates, update)
end
globalUpdates:ListenToNewActiveUpdate(function(id, data)
globalUpdates:LockActiveUpdate(id)
end)
globalUpdates:ListenToNewLockedUpdate(function(id, data)
HandleLockedUpdate(globalUpdates, {id, data})
end)
else
profile:Release()
end
else
player:Kick("Data loading failed, try again.")
end
end
So this is the main issue now, I need to figure out a way to update the statistics as soon as all data is loaded