Datastore2 Issues

I’ve just started messing around with datastore 2 and made my first data set but for some reason it doesn’t save the values I change in game. Any ideas or clues as to what I did wrong?

local dataStore2 = require(1936396537)
local mainKey = "MainKey"
dataStore2.Combine(mainKey, "Stats")

--Data Table
local function SetDataTable()
	local UserData = {
		Stats = {
			["Level"] = 1,
			["Health"] = 23,
			["Strength"] = 1.4,
			["CurrentFloor"] = 2960738307,
			["PlaceName"] = "N/A",
			["Mana"] = 20,
			["MaxMana"] = 20,
			["Lyrics"] = 100,
			["LegacyGems"] = 0
		}
	}
	return UserData
end

--Main
game.Players.PlayerAdded:Connect(function(plr)
	local UserData = dataStore2(mainKey,plr):Get(SetDataTable())
	--PLAYER STATS--
	local stats = Instance.new("Folder")
	stats.Name = "Stats"
	local lvl = Instance.new("IntValue")
	lvl.Name = "Level"
	local hp = Instance.new("IntValue")
	hp.Name = "Health"
	local str = Instance.new("NumberValue")
	str.Name = "Strength"
	local cF = Instance.new("IntValue")
	cF.Name = "CurrentFloor"
	local pN = Instance.new("StringValue")
	pN.Name = "PlaceName"
	local mana = Instance.new("DoubleConstrainedValue")
	mana.Name = "Mana"
	local lyrics = Instance.new("IntValue")
	lyrics.Name = "Lyrics"
	local lGems = Instance.new("IntValue")
	lGems.Name = "LegacyGems"
	
	local StatsData = dataStore2("Stats", plr)
	
	local function UpdateStats(UpdatedValue)
		lvl.Value = StatsData:Get(UpdatedValue).Level
		hp.Value = StatsData:Get(UpdatedValue).Health
		str.Value = StatsData:Get(UpdatedValue).Strength
		cF.Value = StatsData:Get(UpdatedValue).CurrentFloor
		pN.Value = StatsData:Get(UpdatedValue).PlaceName
		mana.MaxValue = StatsData:Get(UpdatedValue).Mana
		
		lGems.Value = StatsData:Get(UpdatedValue).LegacyGems
	end
	
	UpdateStats(UserData.Stats)
	
	StatsData:OnUpdate(UpdateStats)
	
	stats.Parent = plr
	lvl.Parent = stats
	hp.Parent = stats
	str.Parent = stats
	cF.Parent = stats
	pN.Parent = stats
	mana.Parent = stats
	lGems.Parent = stats
	print("Successfully loaded")
end)
1 Like

where did you actually save the table

You can’t just manually change the intvalues from the server by itself…
(Btw, it looks very similar to mines :sweat:, it could be improved later on but I’ll tell you how to change data)

When changing data, you’ll need to change it from the player’s table by first grabbing it like so:

local PlayerData = dataStore2(TheStatKey, ThePlrName):Get()  -- Gets player table

Once you’ve done that, search through it/go through a table as you normally would and change it from there:

local PlayerData = dataStore2(TheStatKey, ThePlrName):Get()  -- Gets player table
PlayerData.Strength = WhateverYouWannaDo -- Changes in table

Once thats finished, you’ll want to set it after you’re finished with it to make sure it saves like so:

local PlayerData = dataStore2(TheStatKey, ThePlrName) -- Gets player table
PlayerData:Get().Strength = WhateverYouWannaDo -- Changes in table
PlayerData:Set(PlayerData:Get()) -- Makes sure its set

And if I’m not mistaken and you’ve correctly typed your datastore script (It can be improved heh :sweat_smile:) then it should save and change. (It should also automatically update the int value since you have that :OnUpdate part)

2 Likes

Thank so much, also yeah I used your youtube tutorial to help me set up the tables, very helpful. So just to clarify, even using a remote event wont allow me to change the value?

You know what, nvm about that question, I figured it out, I’m just being dumb lol.