I’m relatively inexperienced when it comes do datastores, so I don’t know the ins and outs of how datastores work.
I’m making a game that has points and items and I’m using separate datastores to save them. To save the points, I simply used a datastore to save the number value. To save the items, I used an inpairs loop to make a table with a the names of the items and saved that into a separate items datastore.
They were originally supposed to save when the player left the game. The points saved just fine, but the items didn’t save. Because of this, I added a script that saves the players items every 15 seconds, which overloaded the datastore service and caused some delay.
I also made the items save whenever the player bought an item which would work just fine unless the player dropped the item and gave it to someone else, which I kept on purpose. If someone did that, they would get the item back if they rejoined, basically duplicating the item for free. It worked before I made the items save every 15 seconds, most likely because the datastore service wasn’t flooded with requests.
That being said, is there a better way to do this? I can provide my scripts if necessary.
Here’s an example. I don’t really use Roblox’s datastore service but considering you’re a beginner with DataStores this should help. This example only loads data, you’ll have to try experimenting with saving the data when the player leaves the game, this source should help with that.
local dataStoreService = game:GetService("DataStoreService")
local players = game:GetService("Players")
local dataTemplate = {
Money = 0,
Inventory = {"Item1"}
}
players.PlayerAdded:Connect(function(player)
local playerDataStore = dataStoreService:GetDataStore("PlayerData.v1"..player.UserId)
local moneyData = playerDataStore:GetAsync("Money")
local invData = playerDataStore:GetAsync("Inventory")
if moneyData then
-- Create a value and set its value to Money data
else
-- If Data does not exist then set it the default value in data template
end
if invData then
-- Do the same thing you did with moneyData but this time make it a for loop since its a table
else
-- If Data does not exist then set it the default value in data template
end
end)
-- For saving the data, you can use :UpdateAsync when the player is leaving the game. Never use :SetAsync