Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you save values in arrays safely?

Asked by 5 years ago

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.

1local SaveArray = {player.leaderstats.Level.Value, player.Credits.Value, player.XP.Value, player.XPN.Value}
2    Storage:SetAsync(player.UserId, SaveArray)
0
it has to be in order greatneil80 2647 — 5y
1
so change the save array to player.XPN.Value, player.XPN.Value in the table greatneil80 2647 — 5y
0
Thank you ReallyUnikatni 68 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

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.

1local DATASTORE_RETRIES = 3
2local tries = 0
3local success = true
4local data, msg
5repeat
6    tries = tries + 1
7    success, msg = pcall(function() Storage:SetAsync(player.UserId, SaveArray) end)
8    if not success then wait(1) end
9until tries == DATASTORE_RETRIES or success

if value got switched, because each value in the table has no name

1local SaveArray = {Level = player.leaderstats.Level.Value, Credit = player.Credits.Value, XP = player.XP.Value, XPN = player.XPN.Value}

then when it loads,

1if LoadArray then
2level.Value = LoadArray.Level
3credits.Value = LoadArray.Credit
4xpn.Value = LoadArray.XPN
5        xp.Value = LoadArray.XP
6 end
Ad

Answer this question