How do I make this script for DataStore2 save

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? saving the boolvalue and initvalue values

  2. What is the issue? I can’t figure out how to do saving

  3. What solutions have you tried so far? trying things with my older versions of this

local DataStore2 = require(1936396537)
local MainKey = "MainKey"

local function SetDataTable()
	local UserData = {
	Stats = {
	["Tix"] = 0,
	["KOs"] = 0,
	["WOs"] = 0,

	Inv = {
		["TestCap"] = false,
return UserData

	local UserData = DataStore2(MainKey,plr):Get(SetDataTable()) --*ERROR HERE* Error with this line, go recheck you're doing this right
	local Stats ="Folder",plr)
	Stats.Name = "Stats"
	local invfolder ="Folder",plr)
	invfolder.Name = "Inv"
	--Stat IntValues
	local Tix ="IntValue",Stats)
	Tix.Name = "Tix"
	local WOs ="IntValue",Stats)
	WOs.Name = "WOs"
	local KOs ="IntValue",Stats)
	KOs.Name = "KOs"
	--Inventory boolval
	local TestCap ="BoolValue",invfolder)
	TestCap.Name = "TestCap"

Ok so I upon much trial and error I got the folders and values to create themselves within the Player folder but whenever I modify the values (boolvalues and intvalues) and then exit studio it says " [ Data store MainKey was not saved as it was not updated.]" and im not sure how to tell it to save every X seconds or upon user exit, I have tried a few things from my past attempts but none have worked.

Can you show us the player removing function and where you save the data?

DataStore2 has a player removing function build into it so when using it normally people don’t save like that(One of the few downsides to using DataStore2).

When saving your Data you should using :Update(UpdateFunc) and passing the values you want to be updated through the update function. So for example the update function I use is

local function SAVE_DATA(player)
	if CURRENT_SESSION[player] then
		local playerData = DataStore2("PlayerStatsDataV12", player)
			return CURRENT_SESSION[player]

This will obviously not work exactly for you but you can modify it and make it work for you. In your situation, you aren’t updating or changing the values of the data ever that’s why you are getting the message “Data was not saved as it wasn’t updated”. So what I would do is create a save function(Like the one I have above) and call it whenever someone gets their currency, dies, or kills someone. This will update the data every time the Player’s stats change.

In the mean time I would go through DataStore2 API and check out other cool things you can use to manipulate your data.

1 Like

like this?

I’m not sure how Datastore2 works. I think @SkoobiDoobiDoo can help you more than I can.

im new to scripting as a whole so im not sure how to respond to him

	local function UpdateStats(UpdatedValue)
		Tix.Value = StatsData:Get(UpdatedValue).Tix
		KOs.Value = StatsData:Get(UpdatedValue).KOs
		WOs.Value = StatsData:Get(UpdatedValue).WOs
	local function UpdateInv(UpdatedValue)
		TestCap.Value = InvData:Get(UpdatedValue).TestCap

anyways I added that to the end of my script and now it outputs “saved MainKey” when I exit the game regardless of if I modify values or not. However values do not save

1 Like

and I do have the SaveInStudio BoolValue in ServerStorage

Are you checking to make sure the Player does not already have data saved?

    local playerData = DataStore2(MainKey, plr)
    local success, data = pcall(function()
     	return playerStatsData:Get()

	if not data then
    	UserData = playerData:Get()

What this does is get’s the Player’s data table saved. If there is not table save then we set their data table to be the default UserTable(Which I would create a new one for) if they do have data we can update the UserData table to be their data.

Also, why do you have UserData in a function? You can just keep the table out as it is :slight_smile: Also to property retrieve data from the UserData table you should index it like so(Using tix as an example)

local TixVal = UserData["Stats"]["Tix"]

so doing this, to update the Player’s stat values you can use

Tix.Value = UserData["Stats"]["Tix"]

Edit: Are you using a ModuleScript for this?

Edit2: Sorry I kind of read too fast and did not realize some things. I would definitely check out @Theevilem’s post. Managing data in the beginning can be very confusing and his post is beautiful going through step by step on how to do it :slight_smile: I would also look more into tables. Saving Data uses a LOT of tables and it can get very confusing managing everything.


Here, try this, this is a post about how I added DataStore2 into my game (and how I saved all of the values)


thank you, I am heavily modifying your script and every step I take I test the game to be sure it works!

1 Like

Glad I could help in some way, same for @SkoobiDoobiDoo his reply was very insightful :slight_smile: