My multiple value data store doesn't save

This is my script I been trying to find different ways to make multiple value data stores and I came up with this.

local ds = game:GetService(“DataStoreService”):GetDataStore(“1ja91j210na0m01ma91”)

game.Players.PlayerAdded:Connect(function(player)
– creats a basic leaderboard
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local Level = Instance.new(‘IntValue’)
Level.Name = ‘Level’
Level.Parent = leaderstats

local Exp = Instance.new(‘IntValue’)
Exp.Name = ‘Exp’
Exp.Parent = leaderstats

local Dragons = Instance.new(‘IntValue’)
Dragons.Name = ‘DragonCoins’
Dragons.Parent = leaderstats

local Potions = Instance.new(‘IntValue’)
Potions.Name = ‘Potions’
Potions.Parent = leaderstats

local MainQuest = Instance.new(‘IntValue’)
MainQuest.Name = ‘MainQuest’
MainQuest.Parent = leaderstats

local HP = Instance.new(‘IntValue’)
HP.Name = ‘Hp’
HP.Parent = leaderstats

local ST = Instance.new(‘IntValue’)
ST.Name = ‘StrengthPoints’
ST.Parent = leaderstats

local DP = Instance.new(‘IntValue’)
DP.Name = ‘DodgePoints’
DP.Parent = leaderstats

local SkillPoints = Instance.new(‘IntValue’)
SkillPoints.Name = ‘SkillPoints’
SkillPoints.Parent = leaderstats

local SideQuest1 = Instance.new(‘IntValue’)
SideQuest1.Name = ‘Side1’
SideQuest1.Parent = leaderstats

local SideQuest2 = Instance.new(‘IntValue’)
SideQuest2.Name = ‘Side2’
SideQuest2.Parent = leaderstats

local SideQuest3 = Instance.new(‘IntValue’)
SideQuest3.Name = ‘Side3’
SideQuest3.Parent = leaderstats

local SideQuest4 = Instance.new(‘IntValue’)
SideQuest4.Name = ‘Side4’
SideQuest4.Parent = leaderstats

local SideQuest5 = Instance.new(‘IntValue’)
SideQuest5.Name = ‘Side5’
SideQuest5.Parent = leaderstats

local SideQuest6 = Instance.new(‘IntValue’)
SideQuest6.Name = ‘Side6’
SideQuest6.Parent = leaderstats

local SideQuest7 = Instance.new(‘IntValue’)
SideQuest7.Name = ‘Side7’
SideQuest7.Parent = leaderstats

local SideQuest8 = Instance.new(‘IntValue’)
SideQuest8.Name = ‘Side8’
SideQuest8.Parent = leaderstats

local SideQuest9 = Instance.new(‘IntValue’)
SideQuest9.Name = ‘Side9’
SideQuest9.Parent = leaderstats

local SideQuest10 = Instance.new(‘IntValue’)
SideQuest10.Name = ‘Side10’
SideQuest10.Parent = leaderstats

local stats = ds:GetAsync(player.UserId)

if stats ~= nil then
Level.Value = stats[1]
Exp.Value = stats[2]
Dragons.Value = stats[3]
Potions.Value = stats[4]
MainQuest.Value = stats[5]
HP.Value = stats[6]
ST.Value = stats[7]
DP.Value = stats[8]
SkillPoints.Value = stats[9]
SideQuest1.Value = stats[10]
SideQuest2.Value = stats[11]
SideQuest3.Value = stats[12]
SideQuest4.Value = stats[13]
SideQuest5.Value = stats[14]
SideQuest6.Value = stats[15]
SideQuest7.Value = stats[16]
SideQuest8.Value = stats[17]
SideQuest9.Value = stats[18]
SideQuest10.Value = stats[19]
print(“Loaded data”)
else
Level.Value = 0
Exp.Value = 0
Dragons.Value = 0
Potions.Value = 0
MainQuest.Value = 1
HP.Value = 0
ST.Value = 0
DP.Value = 0
SkillPoints.Value = 0
SideQuest1.Value = 0
SideQuest2.Value = 0
SideQuest3.Value = 0
SideQuest4.Value = 0
SideQuest5.Value = 0
SideQuest6.Value = 0
SideQuest7.Value = 0
SideQuest8.Value = 0
SideQuest9.Value = 0
SideQuest10.Value = 0
print(“No data found”)
end
end)

game.Players.PlayerRemoving:Connect(function(player)
local save = {}

table.insert(save, player.leaderstats.Level.Value)
table.insert(save, player.leaderstats.Exp.Value)
table.insert(save, player.leaderstats.DragonCoins.Value)
table.insert(save, player.leaderstats.Potions.Value)
table.insert(save, player.leaderstats.MainQuest.Value)
table.insert(save, player.leaderstats.Hp.Value)
table.insert(save, player.leaderstats.StrengthPoints.Value)
table.insert(save, player.leaderstats.DodgePoints.Value)
table.insert(save, player.leaderstats.SkillPoints.Value)
table.insert(save, player.leaderstats.Side1.Value)
table.insert(save, player.leaderstats.Side2.Value)
table.insert(save, player.leaderstats.Side3.Value)
table.insert(save, player.leaderstats.Side4.Value)
table.insert(save, player.leaderstats.Side5.Value)
table.insert(save, player.leaderstats.Side6.Value)
table.insert(save, player.leaderstats.Side7.Value)
table.insert(save, player.leaderstats.Side8.Value)
table.insert(save, player.leaderstats.Side9.Value)
table.insert(save, player.leaderstats.Side10.Value)

ds:SetAsync(player.UserId, save)
print(“Game successfully saved”)
end)

1 Like

You have to load the data. You are just getting it. Also, you wrap your :GetAsync() in a pcall

2 Likes

I use the method of conjoining the information into parts, the “unloading” the information and reading it.

Here is an example (it should work):

local dataStore = game:GetService("DataStoreService"):GetDataStore("MyData")

game.Players.PlayerAdded:Connect(function(player)
	--create your leaderstats
	
	local dataKey = player.UserId .. "-data"
	local dataTable 
	
	local success, errormessage = pcall(function()
		dataTable = dataStore:GetAsync(dataKey)
	end)
	
	if success and dataTable then
		for _, v in pairs(dataTable) do
			local split = string.split(v, "-")
			local name = split[1]
			local value = split[2]
			
			--set the values
			local foundValue = player.leaderstats:FindFirstChild(name)
			if foundValue  then
				foundValue.Value = value
			end
		end
	else
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local dataKey = player.UserId .. "-data"
	local dataToSave = player.leaderstats:GetChildren()
	
	local data = {}
	for _, v in pairs(dataToSave) do
		local together = v.Name .. "-" .. tostring(v.Value)
		table.insert(data, together)
	end
	
	local success, errormessage = pcall(function()
		dataStore:SetAsync(dataKey, data)
	end)
	
	if success then 
		warn("Saved data for", player.Name)
	else
		warn(errormessage)
	end
end)

game:BindToClose(function()
	wait(8)
end)
3 Likes

I have a feeling it’s so obvious why this code doesn’t work for me but what I understood from your example is that this code should work, but it doesn’t

local dataStore = game:GetService(“DataStoreService”):GetDataStore(“Data”)

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local Level = Instance.new(‘IntValue’)
Level.Name = ‘Level’
Level.Parent = leaderstats

local Exp = Instance.new(‘IntValue’)
Exp.Name = ‘Exp’
Exp.Parent = leaderstats

local Dragons = Instance.new(‘IntValue’)
Dragons.Name = ‘DragonCoins’
Dragons.Parent = leaderstats

local Potions = Instance.new(‘IntValue’)
Potions.Name = ‘Potions’
Potions.Parent = leaderstats

local MainQuest = Instance.new(‘IntValue’)
MainQuest.Name = ‘MainQuest’
MainQuest.Parent = leaderstats

local HP = Instance.new(‘IntValue’)
HP.Name = ‘Hp’
HP.Parent = leaderstats

local ST = Instance.new(‘IntValue’)
ST.Name = ‘StrengthPoints’
ST.Parent = leaderstats

local DP = Instance.new(‘IntValue’)
DP.Name = ‘DodgePoints’
DP.Parent = leaderstats

local SkillPoints = Instance.new(‘IntValue’)
SkillPoints.Name = ‘SkillPoints’
SkillPoints.Parent = leaderstats

local SideQuest1 = Instance.new(‘IntValue’)
SideQuest1.Name = ‘Side1’
SideQuest1.Parent = leaderstats

local SideQuest2 = Instance.new(‘IntValue’)
SideQuest2.Name = ‘Side2’
SideQuest2.Parent = leaderstats

local SideQuest3 = Instance.new(‘IntValue’)
SideQuest3.Name = ‘Side3’
SideQuest3.Parent = leaderstats

local SideQuest4 = Instance.new(‘IntValue’)
SideQuest4.Name = ‘Side4’
SideQuest4.Parent = leaderstats

local SideQuest5 = Instance.new(‘IntValue’)
SideQuest5.Name = ‘Side5’
SideQuest5.Parent = leaderstats

local SideQuest6 = Instance.new(‘IntValue’)
SideQuest6.Name = ‘Side6’
SideQuest6.Parent = leaderstats

local SideQuest7 = Instance.new(‘IntValue’)
SideQuest7.Name = ‘Side7’
SideQuest7.Parent = leaderstats

local SideQuest8 = Instance.new(‘IntValue’)
SideQuest8.Name = ‘Side8’
SideQuest8.Parent = leaderstats

local SideQuest9 = Instance.new(‘IntValue’)
SideQuest9.Name = ‘Side9’
SideQuest9.Parent = leaderstats

local SideQuest10 = Instance.new(‘IntValue’)
SideQuest10.Name = ‘name’
SideQuest10.Parent = leaderstats

–create your leaderstats

local dataKey = player.UserId … “-data”
local dataTable

local success, errormessage = pcall(function()
dataTable = dataStore:GetAsync(dataKey)
end)

if success and dataTable then
for _, v in pairs(dataTable) do
local split = string.split(v, “-”)
local Lvl = split[1]
local XP = split[2]
local DragonCoin = split[3]
local Potion = split[4]
local MainQ = split[5]
local hp = split[6]
local st = split[7]
local Dodge = split[8]
local Skill = split[9]
local side1 = split[10]
local side2 = split[11]
local side3 = split[12]
local side4 = split[13]
local side5 = split[14]
local side6 = split[15]
local side7 = split[16]
local side8 = split[17]
local value = split[18]
local name = split[19]
print(“Loaded Data”)

  	--set the values
  	local foundValue = player.leaderstats:WaitForChild("Level")
  	if foundValue  then
  		foundValue.Value = value
  	end
  end

else
warn(errormessage)
end
end)

game.Players.PlayerRemoving:Connect(function(player)
local dataKey = player.UserId … “-data”
local dataToSave = player.leaderstats:GetChildren()

local data = {}
for _, v in pairs(dataToSave) do
local together = v.Name … “-” … tostring(v.Value)
table.insert(data, together)
end

local success, errormessage = pcall(function()
dataStore:SetAsync(dataKey, data)
end)

if success then
warn(“Saved data for”, player.Name)
else
warn(errormessage)
end
end)

game:BindToClose(function()
wait(8)
end)

Remove and dataTable in the if success and dataTable statement

2 Likes

Use this vid to help

1 Like

Code says it saved successfully but it didn’t or maybe it did but didn’t load properly I’m going to double check my script but if you want to check here it is:

local DSS = game:GetService(“DataStoreService”)
local statsDS = DSS:GetDataStore(“MyData!!!12”)

game.Players.PlayerAdded:Connect(function(plr)

local Folder = Instance.new(“Folder”, plr)
Folder.Name = “leaderstats”

local Level = Instance.new(“IntValue”, Folder)
Level.Name = “Level”

local Exp = Instance.new(“IntValue”, Folder)
Exp.Name = “Exp”

local DragonCoins = Instance.new(“IntValue”, Folder)
DragonCoins.Name = “DragonCoins”

local Potions = Instance.new(“IntValue”, Folder)
Potions.Name = “Potions”

local MainQuest = Instance.new(“IntValue”, Folder)
MainQuest.Name = “MainQuest”

local HP = Instance.new(“IntValue”, Folder)
HP.Name = “Hp”

local ST = Instance.new(“IntValue”, Folder)
ST.Name = “StrengthPoints”

local DP = Instance.new(“IntValue”, Folder)
DP.Name = “DodgePoints”

local SkillPoints = Instance.new(“IntValue”, Folder)
SkillPoints.Name = “SkillPoints”

local Side1 = Instance.new(“IntValue”, Folder)
Side1.Name = “SideQuest1”

local Side2 = Instance.new(“IntValue”, Folder)
Side2.Name = “SideQuest2”

local Side3 = Instance.new(“IntValue”, Folder)
Side3.Name = “SideQuest3”

local Side4 = Instance.new(“IntValue”, Folder)
Side4.Name = “SideQuest4”

local Side5 = Instance.new(“IntValue”, Folder)
Side5.Name = “SideQuest5”

local Side6 = Instance.new(“IntValue”, Folder)
Side6.Name = “SideQuest6”

local Side7 = Instance.new(“IntValue”, Folder)
Side7.Name = “SideQuest7”

local Side8 = Instance.new(“IntValue”, Folder)
Side8.Name = “SideQuest8”

local Side9 = Instance.new(“IntValue”, Folder)
Side9.Name = “SideQuest9”

local Side10 = Instance.new(“IntValue”, Folder)
Side10.Name = “SideQuest10”

local data

local succ, err = pcall(function()
data = statsDS:GetAsync(plr.UserId…"-stats")
end)

if succ then
print(“Success”)
if data then
print(" got the data from player datastore")
Level.Value = data[1]
Exp.Value = data[2]
DragonCoins.Value = data[3]
Potions.Value = data[4]
MainQuest.Value = data[5]
HP.Value = data[6]
ST.Value = data[7]
DP.Value = data[8]
SkillPoints.Value = data[9]
Side1.Value = data[10]
Side2.Value = data[11]
Side3.Value = data[12]
Side4.Value = data[13]
Side5.Value = data[14]
Side6.Value = data[15]
Side7.Value = data[16]
Side8.Value = data[17]
Side9.Value = data[18]
Side10.Value = data[19]
end
else
print("there was an error while checking: "…plr.Name)
warn(err)
end
end)

game.Players.PlayerRemoving:Connect(function(plr)
print(“The player is leaving the game”)
local Level = plr.leaderstats.Level
local Exp = plr.leaderstats.Exp local DragonCoins = plr.leaderstats.DragonCoins
local Potions = plr.leaderstats.Potions
local MainQ = plr.leaderstats.MainQuest
local HP = plr.leaderstats.Hp
local ST = plr.leaderstats.StrengthPoints
local DP = plr.leaderstats.DodgePoints
local SkillPoints = plr.leaderstats.SkillPoints
local Side1 = plr.leaderstats.SideQuest1
local Side2 = plr.leaderstats.SideQuest2
local Side3 = plr.leaderstats.SideQuest3
local Side4 = plr.leaderstats.SideQuest4
local Side5 = plr.leaderstats.SideQuest5
local Side6 = plr.leaderstats.SideQuest6
local Side7 = plr.leaderstats.SideQuest7
local Side8 = plr.leaderstats.SideQuest8
local Side9 = plr.leaderstats.SideQuest9
local Side10 = plr.leaderstats.SideQuest10

local data = {Level, Exp, DragonCoins, Potions, MainQ, HP, ST, DP, SkillPoints, Side1, Side2, Side3, Side4, Side5, Side6, Side7, Side8, Side9, Side10}
print(“Got data time to save to datastore”)

local succ, err = pcall(function()
statsDS:SetAsync(plr.UserId…"-stats",game:GetService(“HttpService”):JSONEncode(data))
end)

if succ then
print(“Successfully saved the data”)
else
print(“There was an error while saving player leaderstats!”)
warn(err)
end
end)

1 Like

make sure you have tested this in a game and that api services are on

I did test the game with api services on and https is https service not making it work? I think it shouldn’t affect at all though I’ll see if it does.

1 Like

I found a really unpopular video with a working data store so the problem is fixed now. Thanks to those who tried to help.

it would be best to actually give out that video here as the answer. that way, people who stumble upon this thread wondering the same thing can actually be helped, rather than seeing a solution that isn’t really a solution.

2 Likes

I didn’t say because I can’t find the vid anymore. Sorry about that it you needed it.

If you want to condense this heavily, I recently replied to someone doing a similar thing. I would recommend checking out DataStore2 to save yourself the headache.

1 Like