Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My simple datastore isn't saving stats, should I migrate to datastore2?

How hard would it be to turn this into datastore2? This script worked fine and now it doesnt..

local DataStore = game:GetService("DataStoreService")
local MaxHealth1 = DataStore:GetDataStore("MaxHealth001")
local Level1 = DataStore:GetDataStore("Level001")
local Qalium1 = DataStore:GetDataStore("Qalium001")
local XP1 = DataStore:GetDataStore("XP001")
local HealthP1 = DataStore:GetDataStore("Health001")
local SwordP1 = DataStore:GetDataStore("Sword001")
local MeleeP1 = DataStore:GetDataStore("Melee001")
local ClassP1 = DataStore:GetDataStore("Class001")
local Points1 = DataStore:GetDataStore("Points001")
local Sign1 = DataStore:GetDataStore("Sign001")
local Tries1 = DataStore:GetDataStore("Tries001")
local Element1 = DataStore:GetDataStore("Element001")

game.Players.PlayerAdded:Connect(function(Plr)
	local stats = Instance.new("Folder", Plr)
	stats.Name = "leaderstats1"
	
	local MaxHealth = Instance.new("IntValue", stats)
	MaxHealth.Name = "MaxHealth"
	MaxHealth.Value = 125
	local Level = Instance.new("IntValue", stats)
	Level.Name = "Level"
	Level.Value = 1
	local XP = Instance.new("IntValue", stats)
	XP.Name = "XP"
	XP.Value = 0
	local Qalium = Instance.new("IntValue", stats)
	Qalium.Name = "Qalium"
	Qalium.Value = 0
	local HealthP = Instance.new("IntValue", stats)
	HealthP.Name = "HealthP"
	HealthP.Value = 1
	local SwordP = Instance.new("IntValue", stats)
	SwordP.Name = "SwordP"
	SwordP.Value = 1
	local MeleeP = Instance.new("IntValue", stats)
	MeleeP.Name = "MeleeP"
	MeleeP.Value = 1
	local ClassP = Instance.new("IntValue", stats)
	ClassP.Name = "ClassP"
	ClassP.Value = 1
	local Sign = Instance.new("StringValue", stats)
	Sign.Name = "Sign"
	Sign.Value = ""
	local Points = Instance.new("IntValue", stats)
	Points.Name = "Points"
	Points.Value = 0
	local Tries = Instance.new("IntValue", stats)
	Tries.Name = "Tries"
	Tries.Value = 0
	local Element = Instance.new("StringValue", stats)
	Element.Name = "Element"
	Element.Value = ""
	
	MaxHealth.Value = MaxHealth1:GetAsync(Plr.UserId) or MaxHealth.Value
	MaxHealth1:SetAsync(Plr.UserId, MaxHealth.Value)
	MaxHealth.Changed:connect(function()
		MaxHealth1:SetAsync(Plr.UserId, MaxHealth.Value)
	end)
   Level.Value = Level1:GetAsync(Plr.UserId) or Level.Value
	   Level1:SetAsync(Plr.UserId, Level.Value)
      Level.Changed:connect(function()
	   Level1:SetAsync(Plr.UserId, Level.Value)
   end)
   Qalium.Value = Qalium1:GetAsync(Plr.UserId) or Qalium.Value
	   Qalium1:SetAsync(Plr.UserId, Qalium.Value)
      Qalium.Changed:connect(function()
	   Qalium1:SetAsync(Plr.UserId, Qalium.Value)
   end)
   XP.Value = XP1:GetAsync(Plr.UserId) or XP.Value
	   XP1:SetAsync(Plr.UserId, XP.Value)
      XP.Changed:connect(function()
	   XP1:SetAsync(Plr.UserId, XP.Value)
   end)
   SwordP.Value = SwordP1:GetAsync(Plr.UserId) or SwordP.Value
	   SwordP1:SetAsync(Plr.UserId, SwordP.Value)
      SwordP.Changed:connect(function()
	   SwordP1:SetAsync(Plr.UserId, SwordP.Value)
	end)
	HealthP.Value = HealthP1:GetAsync(Plr.UserId) or HealthP.Value
	HealthP1:SetAsync(Plr.UserId, HealthP.Value)
	HealthP.Changed:connect(function()
		HealthP1:SetAsync(Plr.UserId, HealthP.Value)
	end)
	MeleeP.Value = MeleeP1:GetAsync(Plr.UserId) or MeleeP.Value
	MeleeP1:SetAsync(Plr.UserId, MeleeP.Value)
	MeleeP.Changed:connect(function()
		MeleeP1:SetAsync(Plr.UserId, MeleeP.Value)
	end)
	ClassP.Value = ClassP1:GetAsync(Plr.UserId) or ClassP.Value
	ClassP1:SetAsync(Plr.UserId, ClassP.Value)
	ClassP.Changed:connect(function()
		ClassP1:SetAsync(Plr.UserId, ClassP.Value)
	end)
	Sign.Value = Sign1:GetAsync(Plr.UserId) or Sign.Value
	Sign1:SetAsync(Plr.UserId, Sign.Value)
	Sign.Changed:connect(function()
		Sign1:SetAsync(Plr.UserId, Sign.Value)
	end)
   Points.Value = Points1:GetAsync(Plr.UserId) or Points.Value
	   Points1:SetAsync(Plr.UserId, Points.Value)
      Points.Changed:connect(function()
	   Points1:SetAsync(Plr.UserId, Points.Value)
	end)
	Tries.Value = Tries1:GetAsync(Plr.UserId) or Tries.Value
	Tries1:SetAsync(Plr.UserId, Tries.Value)
	Tries.Changed:connect(function()
		Tries1:SetAsync(Plr.UserId, Tries.Value)
	end)
	Element.Value = Element1:GetAsync(Plr.UserId) or Element.Value
	Element1:SetAsync(Plr.UserId, Element.Value)
	Element.Changed:connect(function()
		Element1:SetAsync(Plr.UserId, Element.Value)
	end)
end)

game.Players.PlayerRemoving:connect(function(Player)
	MaxHealth1:SetAsync(Player.UserId, Player.leaderstats1.MaxHealth.Value)
	Level1:SetAsync(Player.UserId, Player.leaderstats1.Level.Value)
	Qalium1:SetAsync(Player.UserId, Player.leaderstats1.Qalium.Value)
	XP1:SetAsync(Player.UserId, Player.leaderstats1.XP.Value)
	HealthP1:SetAsync(Player.UserId, Player.leaderstats1.HealthP.Value)
	SwordP1:SetAsync(Player.UserId, Player.leaderstats1.SwordP.Value)
	MeleeP1:SetAsync(Player.UserId, Player.leaderstats1.MeleeP.Value)
	ClassP1:SetAsync(Player.UserId, Player.leaderstats1.ClassP.Value)
	Points1:SetAsync(Player.UserId, Player.leaderstats1.Points.Value)
	Tries1:SetAsync(Player.UserId, Player.leaderstats1.Tries.Value)
	Element1:SetAsync(Player.UserId, Player.leaderstats1.Element.Value)
	Sign1:SetAsync(Player.UserId, Player.leaderstats1.Sign.Value)
end)


1 comment

0

Why do you have a seperate data store for each value?

1 answer

Hell yeah! It sets itself up for garbage collection, it has caching, and it has a backup datastore so it prevents data loss.

article here: https://devforum.roblox.com/t/how-to-use-datastore2-data-store-caching-and-data-loss-prevention/136317

Buuuut just like jasmin said there is no reason to make a separate datastore for each thing, you just have to make a table

1 comment

0

I learned scripting from reading scripts in free models and this is how all the datastores look, I haven't seen anyone use tables, can you elaborate or show me an example using my code?

This discussion is preserved as it was. New answers, comments, and votes are closed.