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

[SOLVED] Why isn't my table datastore saving or loading?

Hello! Here's my datastore script so far. There are no errors, but nothing appears to save or load. All that happens when a user joins the game is a new IntValue with the value 0. When the player leaves, the values are still printed, however using a datastore editor displays no data in the Inventories datastore.

local DS = game:GetService("DataStoreService"):GetDataStore("Inventories")
game.Players.PlayerAdded:Connect(function(player)
	variablePlayer = player.Name
    local folder = Instance.new("Folder",player)
    folder.Name = "inventory"
    inventory = {}
    inventory.Name = "inventory"
    local dataKey = "userkey_"..player.UserId
    local data
    local success,errormsg = pcall(function()
        data = DS:GetAsync(dataKey)
    end)
    if success then
		for i,v in pairs(data) do
			local i2 = Instance.new("IntValue", folder)
			i2.Value = v
		end
	else
        warn("Error when retrieving data for "..player.Name..". Error: "..errormsg)
        folder.Value = nil
    end
    end)
game.Players.PlayerRemoving:Connect(function(player)
    local success,errormsg = pcall(function()
        for i, v in pairs(game.Players[variablePlayer].inventory:GetChildren()) do
			table.insert(inventory, v.Value)
			print(v.Value)
        end
	DS:SetAsync("userkey_"..player.UserId, inventory)
    end)
    if not success then
        warn("Unable to save for "..player.Name..". Error: "..errormsg)
    end
end)

2 comments

0

put: while nil do break end that will fix it

0

Where?

1 answer

I ended up finding a solution, however the solution creates a new problem, which I will make another post for.

local DS = game:GetService("DataStoreService"):GetDataStore("a")
game.Players.PlayerAdded:Connect(function(player)
	variablePlayer = player.Name
    local folder = Instance.new("Folder",player)
    folder.Name = "inventoryfolder"
    inventory = {}
    inventory.Name = "inventory"
    local dataKey = "userkey_"..player.UserId
    local data
    local success,errormsg = pcall(function()
        data = DS:GetAsync(dataKey)
    end)
    if success then
		if data ~= nil then
			for i,v in pairs(data) do
				print(i,v)
				local i2 = Instance.new("StringValue", folder)
				i2.Value = v
			end
		else
			print("Player is new.")
		end	
	else
        warn("Error when retrieving data for "..player.Name..". Error: "..errormsg)
        folder.Value = nil
    end
    end)
game.Players.PlayerRemoving:Connect(function(player)
    local success,errormsg = pcall(function()
	
        for i, v in pairs(player.inventoryfolder:GetChildren()) do
			table.insert(inventory, i, v.Value) -- This is where the issue is, it was just "inventory, v.Value" when it should have been what it is now.
			print(v.Value)

        end

	DS:SetAsync("userkey_"..player.UserId, inventory) -- Now, tables can't be saved as a datastore which will be the new post.
    end)
    if not success then
        warn("Unable to save for "..player.Name..". Error: "..errormsg)
    end
end)

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