How do I save coins and items using datastores?

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.

You want to save player’s items to a datastore right

For the coins you’ll simply just save its value.
(There are many posts about it on the forum).

For the items, you’ll have to save their names/values as string/boolean/number into your data.
There are posts on that aswell.

How do I do that with multiple items?

Like save multiple items to the datastore?

You can use tables, and store the items in there.
There are multiple tutorials on that, both on Youtube and the forum.

That’s what I’m currently doing. My main problem is that items are not saving when the player leaves the game, but points will save just fine

1 Like

You’ll need to clone them from RS or any other folder they are , into the player. For e.g:

You could check if the player owns an item, if he does - clone that item into his inv.

I suggest you to check some tutorials that have been made already on the forum / YouTube.

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
1 Like