How can i fix my DataStore script (Using DataStore2)

So i’ve been looking forward on DataStore2 to learn how it works since people say it is a better efficient way to store data, then i went googling around and found this tutorial: Saving Stats with DataStore2 (DEPRECATED)

I’ve done everything it says on the tutorial but yet it doesn’t work and i don’t get any errors on the output neither.

What i want to do is:

When a player touch a specific part, this part will access the “Gold” Data and give the player +1 on it’s value, but when i check the values on the player nothing changes at all, i manually changed then and even that didn’t work even though it shows in the output that it saved the data ( image )

Here is was my scripts look like:

DataStore Script:

local DataStore2 = require(game:GetService("ServerScriptService").DataStore2) -- Require the DataStore Module using the name
local MainKey = "Key1"

DataStore2.Combine(MainKey, "Stats")

local function CreateDataTable()
	local PlayerData = {
		Stats = {
        
        ["Gold"] = 0;
		["Silver"] = 0;
		["Bronze"] = 0;
		["Rushies"] = 0;
		["Level"] = 1;
		["Exp"] = 0;
		["MaxExp"] = 100;
	};
}
return PlayerData
end

game:GetService("Players").PlayerAdded:Connect(function(plr) -- WHEN PLAYER JOINS --
	local PlayerData = DataStore2(MainKey, plr):Get(CreateDataTable()) -- GetPlayersDataTable
	
	local Folder = Instance.new("Folder") -- CURRENT STATS --
	Folder.Name = "PlayerStats"
	
	local Gold = Instance.new("IntValue")
	Gold.Name = "Gold"
	local Silver = Instance.new("IntValue")
	Silver.Name = "Silver"
	local Bronze = Instance.new("IntValue")
	Bronze.Name = "Bronze"
	local Rushies = Instance.new("IntValue")
	Rushies.Name = "Rushies"
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	local Exp = Instance.new("IntValue")
	Exp.Name = "Exp"
	local MaxExp = Instance.new("IntValue")
	MaxExp.Name = "MaxExp"
	
	local Leaderboard = Instance.new("Folder") -- LEADERSTATS STUFF --
	Leaderboard.Name = "leaderstats"
	
	local LeaderboardWins = Instance.new("IntValue")
	LeaderboardWins.Name = "Wins"
	LeaderboardWins.Value = Gold.Value
	local LeaderboardLv = Instance.new("IntValue")
	LeaderboardLv.Name = "Lv"
	LeaderboardLv.Value = Level.Value


	local StatsData = DataStore2("Stats", plr)
	
	local function UpdateAllStats(UpdatedStats)
	Gold.Value = DataStore2(MainKey, plr):GetTable(UpdatedStats)["Stats"]["Gold"] -- Sets value you've made to correspond with data table
	Silver.Value = DataStore2(MainKey, plr):GetTable(UpdatedStats)["Stats"]["Silver"]
	Bronze.Value = DataStore2(MainKey, plr):GetTable(UpdatedStats)["Stats"]["Bronze"]
	Rushies.Value = DataStore2(MainKey, plr):GetTable(UpdatedStats)["Stats"]["Rushies"]
	Level.Value = DataStore2(MainKey, plr):GetTable(UpdatedStats)["Stats"]["Level"]
	Exp.Value = DataStore2(MainKey, plr):GetTable(UpdatedStats)["Stats"]["Exp"]
	MaxExp.Value = DataStore2(MainKey, plr):GetTable(UpdatedStats)["Stats"]["MaxExp"]
	end
	
	UpdateAllStats(PlayerData.Stats) -- Calls the function and updates player data to appropriate values
    DataStore2(MainKey, plr):OnUpdate(UpdateAllStats) -- Calls when updated

    Folder.Parent = plr
    Gold.Parent = Folder
    Silver.Parent = Folder
    Bronze.Parent = Folder 
    Rushies.Parent = Folder
    Level.Parent = Folder
	Exp.Parent = Folder
	MaxExp.Parent = Folder
	Leaderboard.Parent = plr 
	LeaderboardWins.Parent = Leaderboard
	LeaderboardLv.Parent = Leaderboard
	
	local function UpdateLeaderstats()
		LeaderboardWins.Value = Gold.Value
		LeaderboardLv.Value = Level.Value
	end
	
	Level.Changed:Connect(UpdateLeaderstats)
    Gold.Changed:Connect(UpdateLeaderstats)
	
	Exp.Changed:Connect(function()
		if plr.Stats.Exp.Value >= plr.Stats.MaxExp.Value then
			Level.Value = Level.Value + 1
			Exp.Value = 0
			plr.Stats.MaxExp.Value = plr.Stats.MaxExp.Value+50
		end
	end)
end)

Touched Part Script:

local DataStore2 = require(game:GetService("ServerScriptService").DataStore2)

script.Parent.Touched:Connect(function(hit) 
	local vals = game.ReplicatedStorage.vals
	local Part = script.Parent.Parent.SecondPart
	local h = hit.Parent:FindFirstChild("Humanoid")
	local GainXP = math.random(40, 50)
	local GainRushies = math.random(45, 50)
	if (h~=nil) then
		game.ReplicatedStorage.vals.Winner.Value = hit.Parent.Name
		local plr = game.Players[vals.Winner.Value]
		local plrChar = game.Players:GetPlayerFromCharacter(hit.Parent)
		local plrID = plrChar.UserId
		local plrStats = DataStore2("Stats", plr):Get()
		plrStats.Gold = plrStats.Gold + 1
		plrStats.Exp = plrStats.Exp + GainXP
		plrStats.Rushies = plrStats.Rushies + GainRushies
		DataStore2("Stats", plr):Set(plrStats)
		game.ReplicatedStorage.vals.Winner.Value = hit.Parent.Name
		game.ReplicatedStorage.vals.Winner.WinnerID.Value = plrID
		script.Parent.Parent.StartingPoles.LeftLight.Color = Color3.fromRGB(255, 255, 255)
		script.Parent.Parent.StartingPoles.LeftLight.PointLight.Color = Color3.fromRGB(255, 255, 255)
		script.Parent.Parent.StartingPoles.RightLight.Color = Color3.fromRGB(255, 255, 255)
		script.Parent.Parent.StartingPoles.RightLight.PointLight.Color = Color3.fromRGB(255, 255, 255)
		script.Parent.Parent.CheckerFlag.BeamPart.Attachment.Beam.Color = ColorSequence.new({
			ColorSequenceKeypoint.new(0, Color3.fromRGB(255, 255, 255)),
			ColorSequenceKeypoint.new(1, Color3.fromRGB(255, 255, 255))
		  }
		)
		print(plrID)
		plr.Character.Parent = workspace
	 	Part.Position = script.Parent.Position
		print(game.ReplicatedStorage.vals.Winner.Value)
		script.Parent:Destroy()
		wait(plr:LoadCharacter())
	end
end)

I’ve activated datastorage in roblox studio and already played the published game but nothing worked before anyone asks

1 Like

There’s a function in DataStore2 called GetTable that will make your life easier, and you also should be using it.

https://kampfkarren.github.io/Roblox/api/#datastoregettable

2 Likes

I am sorry but how should i use this function? i am new to DataStorage so i don’t believe i understand where i should use it or how to use it

Sorry about that, forgot that post even existed in the first place, if you’re wondering, that post is “deprecated”.
However, I think it might work still though I don’t recommend it in my opinion? However, if you want to see it save in studio, make sure you’ve inserted a boolvalue called SaveInStudio in ServerStorage to true to test if your datastore saves! and Enabled Studio Acess to API Services.

Oh, i didn’t know it was deprecated OOF. Also i’ve done that already but yet i am getting no results, really struggling to understand how i should do it =/

I managed to fix the issue where the values wouldn’t change if touched the part, but yet the data doesn’t save when i leave and come back, if you or someone else could help me figure out why this happens i would be really thankfull (also i still don’t get where i should use GetTable)