I have been trying to save values with an array but values get switched or they just don't save.
This is when the player joins the game.
01 | -- Loading Data |
02 |
03 | local success, result = pcall ( function () |
04 | Storage:GetAsync(player.UserId) |
05 | end ) |
06 |
07 | if not success then |
08 | warn(result) |
09 | else |
10 | local LoadArray = Storage:GetAsync(player.UserId) |
11 | if LoadArray then |
12 | level.Value = LoadArray [ 1 ] |
13 | credits.Value = LoadArray [ 2 ] |
14 | xpn.Value = LoadArray [ 3 ] |
15 | xp.Value = LoadArray [ 4 ] |
16 | end |
17 | end |
This is when the player leaves.
1 | local SaveArray = { player.leaderstats.Level.Value, player.Credits.Value, player.XP.Value, player.XPN.Value } |
2 | Storage:SetAsync(player.UserId, SaveArray) |
if value is not save, that is because Datastore is a service relying on Roblox server's performance. if the network is jammed, it will not save successfully. instead, you need to use the pcall function to save.
1 | local DATASTORE_RETRIES = 3 |
2 | local tries = 0 |
3 | local success = true |
4 | local data, msg |
5 | repeat |
6 | tries = tries + 1 |
7 | success, msg = pcall ( function () Storage:SetAsync(player.UserId, SaveArray) end ) |
8 | if not success then wait( 1 ) end |
9 | until tries = = DATASTORE_RETRIES or success |
if value got switched, because each value in the table has no name
1 | local SaveArray = { Level = player.leaderstats.Level.Value, Credit = player.Credits.Value, XP = player.XP.Value, XPN = player.XPN.Value } |
then when it loads,
1 | if LoadArray then |
2 | level.Value = LoadArray.Level |
3 | credits.Value = LoadArray.Credit |
4 | xpn.Value = LoadArray.XPN |
5 | xp.Value = LoadArray.XP |
6 | end |