I don’t want to give you misleading info/things I’m not 100% on but I’m pretty sure, after reading the thread, you’d just call Set() and within the parameter provide your table. Save() is automatically called upon the player leaving. You also want to load the data upon the player entering the game (PlayerAdded event). Hope this helps!
As an example, let’s say you wanted to save a player’s inventory, but you wanted to optimize the save. For example, if a player has an inventory of {"Item1", "Item2", "Item3"} , and you want to save it as {1, 2, 3} . You can do this easily with these functions. Your code may look like this.
-Kampfkarren
Well, he doesnt explain how to do it, only how to optimize a table, nothing even with dictionaries.
-- saving table
local saveTable = {
1, 2, 3, 4
}
-- reading dictionary
local readDictionary = {
[1] = { -- data for 1};
[2] = { -- data for 2 and so on};
}
Saves a bunch of characters away(limit is 260k, but it is closely to unlikely to hit that limit). Perhaps saving the dictionary directly will do, until you find out that your game is saving a lot of data.
Yes, it would work. But I think it is easier just to save the dictionary in the DataStore and if you are worried about the saving limit I could just JSON-encode the table and then save it.
I have no clue what you’re talking about. DataStore2 is just a wrapper for DataStoreService with extra functionality like data backups, combined datastores, as well as QOL features like getting data with a default value.
There is no difference when saving dictionaries. Like normal DataStores, there can only be string keys.
if I’m correct, you’re trying to save a collection of data (the dictionary) under one datastore, instead of saving single values to multiple datastores. Here’s an example of how you might do that (includes retrieving the data, modifying and setting the data):
-- Services
local players = game:GetService("Players")
local dataStore2 = require(1936396537)
local userDataStoreName = "UserDataStore"
--Get data (retrieves previous data or sets-up new data using the setupUserData function)
local function setupUserData()
local userData = {
}
return userData
end
local userData = dataStore2(userDataStoreName, player):Get(setupUserData())
--Modify data
userData["Currency"]["Gems"] = 9999
--Set data
dataStore2(userDataStoreName, player):Set(userData)
Make sure to replace local userData = {} in the setupUserData function with your dictionary.
Hi, this might be a silly error, but somehow I dont manage to save the values off: userdata["Currency"]["Gems"] (I can change them, but not save them for some reason.
This is my full script:
– Services
local players = game:GetService(“Players”)
local dataStore2 = require(game.Workspace.MainModule)
local userDataStoreName = “UserDataStore”
--Get data (retrieves previous data or sets-up new data using the setupUserData function)
local function setupUserData()
local userData = {
Currency = {
["Gems"] = 15;
["Coins"] = 1500;
};
Items = {
["Weapons"] = {
["Knife"] = {nil};
["AK-47"] = {nil};
["M9"] = {nil};
};
["Clothing"] = {nil};
};
WeaponsEquipped = {
["Main"] = {"AK-47"};
["Secondary"] = {"M9"};
["Melee"] = {"Knife"};
["Special"] = {nil};
};
ClothingEquipped = {
["Pants"] = {nil};
["Hats"] = {nil};
["Shirts"]= {nil};
["Face"] = {nil};
};
}
return userData
end
game.Players.PlayerAdded:Connect(function(player)
local userData = dataStore2(userDataStoreName, player):Get(setupUserData()) --skrev userdata = userdata noe sånt sjekk!
print(userData["Currency"]["Gems"])
end)
game.Workspace.Baseplate.ClickDetector.MouseClick:Connect(function(player)
local userData = dataStore2(userDataStoreName, player)
userData["Currency"]["Gems"] = userData["Currency"]["Gems"] + 10
dataStore2(userDataStoreName, player):Set(setupUserData())
print(userData["Currency"]["Gems"]) [[--this prints the gems value (incremented by 10 --everytime i click)]]--
end)
I just made this code to check if it saves, but it doesnt Would really appreciate it if you would help me! And dont know if the error is in the save or the retrieving of data but yeah.