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)
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”