I’m having trouble with loading data from server to client properly with ProfileService.
I use a remote to communicate with server to client by using PlayerAdded. In PlayerAdded I fire a remote that updates all data on client. Problem is sometimes the data is nil, but if I add wait with a couple second, it works fine, or if I update randomly in the game it works.
How do I make it so that as soon as the data from profile service is loaded, I can fire the remote?
function updateStats()
for _, p in pairs(Players:GetPlayers()) do
local playerData = DataManager:GetData(p)
if Data[p.Name] then
continue
end
Data[p.Name] = playerData
end
UpdateStats:Fire(BridgeNet2.AllPlayers(), Data)
end
function onPlayerAdded(player: Player)
updateStats()
end
I believe this is because you’re using a module script that immediately asks the server to update the data store, and doesn’t allow you to check if it’s been a success or not. You’re doing everything in async, essentially.
Normally you’d be doing smth like:
-- assume the variable DS is your datastore.
data, done = pcall(function()
DS:GetAsync("Level", 8165403); -- just a random example
end)
if done and data ~= nil then
YourRemote:Fire(data);
end
In your case you’re missing that sort of “waiting” part allowing you to check everything’s been fine.
How would you solve it? I see you’ve gottten a part going like this:
if Data[p.Name] then
continue
end
This is almost nice. This won’t stop the not-loaded-yet data to go to the client. You’ll have to stop it for a few seconds before sending. Maybe something like this:
while Data[p.name] == nil and dataTimer >= 0 do
task.wait(1);
dataTimer -= 1;
end
if Data[p.name] then
--fire your remote
else
--data couldn't be reached. You either try again or warn/force the player to rejoin.
end