Saving loads of data

So I’m trying to create upgrades in my game, but I want them to save so first I tought just make a bunch of values in the player, but I did not know if that was the best idea because I had to do like LuckUpgrades and MaxLuckUpgrades for all the upgrades is there another way or a way I can just put a name in a table like this with a module script or something

                                           Name            Value
                                            |                |
local valueTable = {["LuckUpgrades", 5'],["MaxLuckUpgrades], 10"]}
4 Likes

You could also create a folder inside the player when they join the game, and put bunch of values inside of that folder.

1 Like

I have that but idk if thats a good idea, is it?

1 Like

As of my knowledge, that’s what most people do.

1 Like

but how do I make it more simple to add a value like with a table i showed you.

1 Like

For example, make a table like this

local valuesTable = {
	["LuckUpgrades"] = 5,
	["MaxLuckUpgrades"] = 10,
	["CoolCoins"] = 0,
	["Rubies"] = 20
}

Then, first, create a folder where all the values go, loop through the table, and create a new IntValue for each value

local valuesFolder = Instance.new("Folder")
valuesFolder.Name = "Values"
valuesFolder.Parent = player

for name, value in pairs(valuesTable) do
	local newValue = Instance.new("IntValue")
	newValue.Name = name
	newValue.Value = value
	newValue.Parent = valuesFolder
end
1 Like

Well this looks great but Im new to scripting but I need it to save to so I could also do a loop with the data saving right?

1 Like

When you are going to save, you can loop through the valuesFolder, change them back to a table, and save them.

function valuesToTable(valuesFolder)
	local tbl = {}

	for _, intValue in pairs(valuesFolder) do
		tbl[intValue.Name] = intValue.Value
	end

	return tbl
end

function saveData(player)
	local valuesFolder = player:FindFirstChild("Values")

	if valuesFolder then
		local dataToSave = valuesToTable(valuesFolder)

		local success, err
		local attempt = 1
			
		repeat
			success, err = pcall(function()
				DataStore:SetAsync(player.UserId, dataToSave)
			end)

			attempt += 1
		until success or attempt > 4

		if success then
			print(player.Name .. "'s data was success fully saved")
		else
			print(player.Name .. "'s data failed to save")
		end
	end
end

Players:PlayerRemoving(saveData)
1 Like

I got this error
ServerScriptService.leaderstats:25: invalid argument #1 to ‘pairs’ (table expected, got Instance)

at the part I marked

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local valuesTable = {
		["LuckUpgrades"] = 5,
		["MaxLuckUpgrades"] = 10,
	}

	local valuesFolder = Instance.new("Folder")
	valuesFolder.Name = "Values"
	valuesFolder.Parent = player

	for name, value in pairs(valuesTable) do
		local newValue = Instance.new("IntValue")
		newValue.Name = name
		newValue.Value = value
		newValue.Parent = valuesFolder
	end

	function valuesToTable(valuesFolder)
		local tbl = {}

		for _, intValue in pairs(valuesFolder) do ---- HERE IS THE ERROR
			tbl[intValue.Name] = intValue.Value
		end

		return tbl
	end
end)

players.PlayerRemoving:Connect(function(player)
	local valuesFolder = player:FindFirstChild("Values")

	if valuesFolder then
		local dataToSave = valuesToTable(valuesFolder)

		local success, err
		local attempt = 1

		repeat
			success, err = pcall(function()
				DataStoreService:SetAsync(player.UserId, dataToSave)
			end)

			attempt += 1
		until success or attempt > 4

		if success then
			print(player.Name .. "'s data was success fully saved")
		else
			print(player.Name .. "'s data failed to save")
		end
	end
end)

change valuesFolder to valuesFolder:GetChildren()

Now it says that the data failed to save and I have Api thing on

try

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local valuesTable = {
		["LuckUpgrades"] = 5,
		["MaxLuckUpgrades"] = 10,
	}

	local valuesFolder = Instance.new("Folder")
	valuesFolder.Name = "Values"
	valuesFolder.Parent = player

	local data
	local success, err = pcall(function()
		data = myDataStore:GetAsync(player.UserId)
	end)

	for name, value in pairs(valuesTable) do
		local newValue = Instance.new("IntValue")
		newValue.Name = name
		newValue.Value = value
		newValue.Parent = valuesFolder

		if success and data[name] then
			newValue.Value = data[name]
		end
	end
end)

function valuesToTable(valuesFolder)
	local tbl = {}

	for _, intValue in pairs(valuesFolder:GetChildren()) do ---- HERE IS THE ERROR
		tbl[intValue.Name] = intValue.Value
	end

	return tbl
end

players.PlayerRemoving:Connect(function(player)
	local valuesFolder = player:FindFirstChild("Values")

	if valuesFolder then
		local dataToSave = valuesToTable(valuesFolder)

		local success, err
		local attempt = 1

		repeat
			success, err = pcall(function()
				myDataStore:SetAsync(player.UserId, dataToSave)
			end)

			attempt += 1
			task.wait(2)
		until success or attempt > 4

		if success then
			print(player.Name .. "'s data was success fully saved")
		else
			print(player.Name .. "'s data failed to save")
		end
	end
end)

this error attempt to index nil with ‘MaxLuckUpgrades’

and it noly created maxluckupgrades

change if success and data[name] then to if success and data and data[name] then

I dont see: if success and data[name] then
anywhere in the script

Sorry I dint look good but now it doesnt nothing it doesnt say it saved the data or not and it doesnt save it

try adding this code at the end of the script

game:BindToClose(function()
	task.wait(2)
end)

And make sure to change the values like LuckUpgrade through the server.
You can make changes through the server in test play by going to TEST > Current: Client

One more question I tried adding leaderstats to but it dint work and only the leaderstats save can you please take alook at it but if not thats ok:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local leaderstatsTable = {
		["Steps"] = 0,
		["Money"] = 0,
	}
	
	local valuesTable = {
		["LuckUpgrades"] = 5,
		["MaxLuckUpgrades"] = 10,
	}
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local valuesFolder = Instance.new("Folder")
	valuesFolder.Name = "Values"
	valuesFolder.Parent = player

	local data
	local success, err = pcall(function()
		data = myDataStore:GetAsync(player.UserId)
	end)
	
	for name, value in pairs(leaderstatsTable) do
		local newValue = Instance.new("IntValue")
		newValue.Name = name
		newValue.Value = value
		newValue.Parent = leaderstats

		if success and data and data[name] then
			newValue.Value = data[name]
		end
	end

	for name, value in pairs(valuesTable) do
		local newValue = Instance.new("IntValue")
		newValue.Name = name
		newValue.Value = value
		newValue.Parent = valuesFolder

		if success and data and data[name] then
			newValue.Value = data[name]
		end
	end
end)

function leaderstatsToTable(leaderstatsTable)
	local tbl = {}

	for _, intValue in pairs(leaderstatsTable:GetChildren()) do
		tbl[intValue.Name] = intValue.Value
	end

	return tbl
end

function valuesToTable(valuesFolder)
	local tbl = {}

	for _, intValue in pairs(valuesFolder:GetChildren()) do
		tbl[intValue.Name] = intValue.Value
	end

	return tbl
end

players.PlayerRemoving:Connect(function(player)
	local leaderstatsFolder = player:FindFirstChild("leaderstats")
	local valuesFolder = player:FindFirstChild("Values")

	if valuesFolder and leaderstatsFolder then
		local dataToSave = valuesToTable(valuesFolder) and leaderstatsToTable(leaderstatsFolder)

		local success, err
		local attempt = 1

		repeat
			success, err = pcall(function()
				myDataStore:SetAsync(player.UserId, dataToSave)
			end)

			attempt += 1
			task.wait(2)
		until success or attempt > 4

		if success then
			print(player.Name .. "'s data was success fully saved")
		else
			print(player.Name .. "'s data failed to save")
		end
	end
end)

game:BindToClose(function()
	task.wait(2)
end)
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local myDataStoreLeaderstats = DataStoreService:GetDataStore("myDataStoreLeaderstats")
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local leaderstatsTable = {
		["Steps"] = 0,
		["Money"] = 0,
	}
	
	local valuesTable = {
		["LuckUpgrades"] = 5,
		["MaxLuckUpgrades"] = 10,
	}
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local valuesFolder = Instance.new("Folder")
	valuesFolder.Name = "Values"
	valuesFolder.Parent = player

	local data
	local success, err = pcall(function()
		data = myDataStoreLeaderstats:GetAsync(player.UserId)
	end)
	
	for name, value in pairs(leaderstatsTable) do
		local newValue = Instance.new("IntValue")
		newValue.Name = name
		newValue.Value = value
		newValue.Parent = leaderstats

		if success and data and data[name] then
			newValue.Value = data[name]
		end
	end

	data
	success, err = pcall(function()
		data = myDataStore:GetAsync(player.UserId)
	end)

	for name, value in pairs(valuesTable) do
		local newValue = Instance.new("IntValue")
		newValue.Name = name
		newValue.Value = value
		newValue.Parent = valuesFolder

		if success and data and data[name] then
			newValue.Value = data[name]
		end
	end
end)

function valuesToTable(valuesFolder)
	local tbl = {}

	for _, intValue in pairs(valuesFolder:GetChildren()) do
		tbl[intValue.Name] = intValue.Value
	end

	return tbl
end

players.PlayerRemoving:Connect(function(player)
	local leaderstatsFolder = player:FindFirstChild("leaderstats")
	local valuesFolder = player:FindFirstChild("Values")

	if valuesFolder and leaderstatsFolder then
		local dataToSave = valuesToTable(valuesFolder)
		local dataToSaveLeaderstats = valuesToTable(leaderstatsFolder)

		local success, err
		local attempt = 1

		repeat
			success, err = pcall(function()
				myDataStore:SetAsync(player.UserId, dataToSave)
				myDataStoreLeaderstats:SetAsync(player.UserId, dataToSaveLeaderstats)
			end)

			attempt += 1
			task.wait(2)
		until success or attempt > 4

		if success then
			print(player.Name .. "'s data was success fully saved")
		else
			print(player.Name .. "'s data failed to save")
		end
	end
end)

game:BindToClose(function()
	task.wait(2)
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.