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

How do I save a table of a player's backpack in a datastore?

I know there was already a question somewhat like this, but I didn't fully understand the answer. The error that I get from this script is that you can't store an array in datastore. So how would I store a table of the player's backpack in a datastore?

local DSS = game:GetService("DataStoreService")

local datastore = DSS:GetDataStore("GeneralSaveData", "Players")

function generateDataKey(player)
    local ret = "uid_" .. player.userId
    return ret
end

function generateDataTable(player)
    local dataTable = {}
    for _, tool in ipairs(player.Backpack:GetChildren()) do
        table.insert(dataTable, tool.Name)
    end
    return dataTable
end


function saveDataForPlayer(player)
    local key = generateDataKey(player)
    local data = generateDataTable(player)
	datastore:SetAsync(key, data)
end

function inputDataToPlayer(player, data)
if data then
for _,T in pairs(data) do
    local wep = game.ServerStorage.Weapons:FindFirstChild(T)
    if wep then
        wep:Clone().Parent = player.StarterGear
        wep:Clone().Parent = player.Backpack
    end
end

end
end
function loadDataForPlayer(player)
    local key = generateDataKey(player)
    local data = datastore:GetAsync(key)
    inputDataToPlayer(player, data)
end

game.Players.PlayerAdded:Connect(function(player)
    loadDataForPlayer(player)
    end)

game.Players.PlayerRemoving:Connect(saveDataForPlayer) 

There's probably other errors in the code that I didn't notice, so if you see any feel free to correct them.

1 comment

2

Store the name of the object; you cannot save instances to datastores

1 answer

Datastores can handle lists/tables (so long as you don't mix string-keys and integers in the same table), but they cannot handle Instances. I see that your script is already prepared to handle the datastore saving the names of tools (for when you load them, I mean), you just need to convert the player's Backpack's children to a list of names:

function generateDataTable(player)
	local dataTable = {}
	for _, tool in ipairs(player.Backpack:GetChildren()) do
		--might need an if statement here so that you only add to dataTable if 'tool' is something you want them to be able to save (ex if it's a gear item they brought with them, you wouldn't want only want that saved)
		table.insert(dataTable, tool.Name)
	end
	return dataTable
end

Other notes:

On line 30 you use a , to try and save a weapon in two places, but that's not how lua/Roblox work - you have to break it down more:

wep.Parent = player.StarterGear
wep:Clone().Parent = player.Backpack

--Also, to prevent having to find the weapon 2x, your loop could be written:

for _,T in pairs(data) do
	wep = game.ServerStorage.Weapons:FindFirstChild(T)
	if wep then
		wep:Clone().Parent = player.StarterGear
		wep:Clone().Parent = player.Backpack
	end
end

You may also want to improve your script to use pcall around GetAsync/SetAsync, in case they fail (though this does complicate things).

2 comments

0

It worked at first, but now it appears to be broken. I edited my question to what my script is now, what's wrong with it?

0

I looked over the script, but can't see anything wrong just by reading it. If there are no errors, you'll want to start "print"ing stuff out to see what runs and what value relevant variables are. A particular place to check is the "if wep then" -- try printing out what 'T' and 'wep' are. If 'wep' is always nil, you'd know that there is nothing named whatever T equals in ServerStorage.Weapons.

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