Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you save values in arrays safely?

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)

3 comments

0

it has to be in order

1

so change the save array to player.XPN.Value, player.XPN.Value in the table

0

Thank you

1 answer

✓ Accepted answer

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 
 

This discussion is preserved as it was. New answers, comments, and votes are closed.