Help with Datastore 2 inventory

Hey guys! I am trying to make a saving inventory with datastore2.

I am very new to datastore2, but it is a very important module so I am trying my best to learn it.

Is the following code a functional example of a saving inventory? If not, how can I fix it?

Are there any gaps in the code that I can fill?

Thanks!

local DefaultValue = {
	Gears = {},
	Cosmetics = {},
}

local DataStore2 = require(script.Parent:WaitForChild("DataStore2"))

game.Players.PlayerAdded:Connect(function(Player)
	local CharacterStorage = DataStore2("Character", Player)
	Data = CharacterStorage:Get() -- this loads the data
	
	while true do
		wait(200)
		CharacterStorage:Set(DefaultValue)
	end
end)


game.Players.PlayerRemoving:Connect(function(Player)
	local CharacterStorage = DataStore2("Character", Player)
	CharacterStorage:Set(DefaultValue) -- this saves the data
end)

game:BindToClose(function()
	for _, Player in pairs(game.Players:GetPlayers()) do
		local CharacterStorage = DataStore2("Character", Player)
		CharacterStorage:Set(DefaultValue) -- this saves the data
	end
end)

It is erroring…

local DefaultValue = {
	Gears = {0};
	Cosmetics = {0};
}

local DatastoreName = "Datastore"

local PlayerData = {}

local DataStore2 = require(script.Parent:WaitForChild("DataStore2"))

game.Players.PlayerAdded:Connect(function(Player)
	local Storage = DataStore2(DatastoreName, Player)
	PlayerData[Player.UserId] = Storage:Get() or DefaultValue -- this loads the data
	
	print(#PlayerData[Player.UserId].Gears)
	
	while wait(1) do
		table.insert(PlayerData[Player.UserId], math.random(1,102994296))
	end
	
	while true do
		wait(200)
		Storage:Set(PlayerData[Player.UserId])
	end
end)


game.Players.PlayerRemoving:Connect(function(Player)
	local Storage = DataStore2(DatastoreName, Player)
	Storage:Set(PlayerData[Player.UserId]) -- this saves the data
end)

game:BindToClose(function()
	for _, Player in pairs(game.Players:GetPlayers()) do
		local Storage = DataStore2(DatastoreName, Player)
		Storage:Set(PlayerData[Player.UserId]) -- this saves the data
	end
end)

“error when player left! Invalid at input.Cosmetics because: Mixed Array/Dictionary”

1 Like

I don’t use DS2 personally, but a mixed array means there are some numerical indexes and some string indexes.

Example of a mixed dictionary:

local myDictionary = {
    [1] = 'Cool!';
    ['String'] = 'Ok!';
    [23] = 'Words';
}

Example of a normal dictionary:

local myDictionary = {
    ['Item1'] = 'ItemNameOne';
    ['Item2'] = 'ItemNameTwo';
}

Example of a normal table:

local myTable = {
    'Item';
    'Coin';
    'Ok';
}

You would have to resort to using the second or third option as mixed dictionaries aren’t allowed in datastores AFAIK.

You should also move this to #help-and-feedback:scripting-support

1 Like

This is what the table looks like upon saving…

local Table = {
Gears = {1,2,3,4,5},
Cosmetics = {1,2,3,4,5},
}

Is this a mixed table? If so how? How can I make this not a mixed table?

1 Like

Pretty sure that would be considered a mixed table (not 100% sure).

Try doing something like this:

local Table = {
    Gears = {
        ['1'] = 1;
        ['2'] = 2;
        ['3'] = 3;
    };
    Cosmetics = {
        ['1'] = 1;
        ['2'] = 2;
        ['3'] = 3;
    };
}
1 Like

Oh, ok. I understand now.
How does inserting a new value work? It’s Table[String]=Value?

1 Like

Yeah, exactly. It is kind of the same for removing values, in which you would do Table[String] = nil

1 Like