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 4 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.

-- Loading Data

    local success, result = pcall(function()
        Storage:GetAsync(player.UserId)
    end)

    if not success then
        warn(result)
    else
        local LoadArray = Storage:GetAsync(player.UserId)
        if LoadArray then
            level.Value = LoadArray[1]
            credits.Value = LoadArray[2]
            xpn.Value = LoadArray[3]
            xp.Value = LoadArray[4]
        end
    end

This is when the player leaves.

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

1 answer

Log in to vote
1
Answered by 4 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.

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

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

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

then when it loads,


if LoadArray then level.Value = LoadArray.Level credits.Value = LoadArray.Credit xpn.Value = LoadArray.XPN xp.Value = LoadArray.XP end
Ad

Answer this question