Datastore doesn't save for a number of people

Hello, I recently added a new datastore to my roleplay game and now saving does not work for a number of people for some reason. I don’t know what’s causing it and the console does not return any errors.

local module = {}
function module.SavePlayerData(player)
	-- PLAYER DATA
	local playerData = false
	local dataStore = game:GetService("DataStoreService"):GetDataStore(script.Parent.DataStoreVersion.Value)
	local bigTable = {}
	if player.character then
		local v = game.ReplicatedStorage.PlayerData:FindFirstChild(tostring(player.UserId))
		for i,v in pairs(game.ReplicatedStorage.PlayerData:GetChildren()) do
		if v.Name ~= "Folder" and v:IsA("Folder") then
			-- CRIMINAL DATA
			local criminalData = {}
			for l,z in pairs(v.criminalData:GetChildren()) do
				local record = {
					{officer = z.officer.Value,
						report = z.report.Value,
						date = z.date.Value,
						ticketPaid = z.ticketPaid.Value},
				}
				table.insert(criminalData,record)
			end

			-- BOAT DATA
			local boatTable = {}
			for l,z in pairs(v.boatData:GetChildren()) do
				local boat = {
					{model = z.model.Value,
						tag = z.tag.Value,
						color = z.color.Value}
				}
				table.insert(boatTable,boat)
			end

			-- WEIGHTLIFTING DATA
			local weightTable = {}
			for l,z in pairs(v.weightLifting:GetChildren()) do
				local lift = {
					name = z.Name, value = z.Value
				}
				table.insert(weightTable,lift)
			end

			-- BOOKMARK DATA
			local bookmarkData = {}
			if not v:FindFirstChild("bookmarks") then
				local folder = Instance.new("Folder",v)
				folder.Name = "bookmarks"
			end
			for l,z in pairs(v.bookmarks:GetChildren()) do
				table.insert(bookmarkData,z.Value)
			end

			-- CONTACT DATA
			local contactData = {}
			if not v:FindFirstChild("contacts") then
				local folder = Instance.new("Folder",v)
				folder.Name = "contacts"
			end
			for l,z in pairs(v.contacts:GetChildren()) do
				table.insert(contactData,z.Value)
			end

			-- CAR DATA
			local cars = {}
			for l,z in pairs(v.carData:GetChildren()) do
				local car = {
					{model = z.model.Value,
						plate = z.plate.Value,
						gas = z.gas.Value,
						color = z.color.Value,
						underglow = z.underglow.Value},
				}
				table.insert(cars,car)
			end

			-- TUTORIAL DATA
			local tutorialCompleteV = ""
			if v.tutorialComplete.Value == true then
				tutorialCompleteV = "true"
			else
				tutorialCompleteV = "false"
			end

			local placeables = {}
			-- HOUSE DATA
			if game.Workspace.Housing:FindFirstChild(v.address.Value) then
				local house = game.Workspace.Housing:FindFirstChild(v.address.Value)
				if house:FindFirstChild("Placeables") then
					for l,z in pairs(v.houseData:GetChildren()) do
						z:Destroy()
					end
					for l,z in pairs(house.Placeables:GetChildren()) do
						if z:FindFirstChild("model") and z:FindFirstChild("extraInfo") then
							local folder = Instance.new("Folder",v.houseData)
							folder.Name = "placeable"
							local value1 = Instance.new("StringValue",folder)
							value1.Value = z.model.Value
							value1.Name = "model"
							local value2 = Instance.new("StringValue",folder)
							value2.Value = z.extraInfo.Value
							value2.Name = "extraInfo"
						end
					end
					if v.houseData then
						for l,z in pairs(v.houseData:GetChildren()) do
							local placeable = {
								{extraInfo = z.extraInfo.Value,
									model = z.model.Value,
								},
							}
							table.insert(placeables,placeable)
						end
					end
				end
			end

			--houseData = placeables, FOR HOUSE DATA IN TABLE
			--address = v.address.Value FOR ADDRESS DATA IN TABLE
			-- UPDATE STORETABLE
			local storeTable = nil
			if v.names:FindFirstChild("fname") and v.names:FindFirstChild("lname") then
				storeTable = {weightData = weightTable, tutorialComplete = tutorialCompleteV, contacts = contactData, phoneNumber = v.phoneNumber.Value, boatData = boatTable, bookmarks = bookmarkData, jail = v.jailTime.Value, driversLicense = v.driversLicense.Value, firearmsLicense = v.firearmsLicense.Value, huntingLicense = v.huntingLicense.Value, userId = v.Name, callsign = v.callsign.Value, criminalRecords = criminalData, carData = cars, gender = v.gender.Value, money = v.money.Value, fname = v.names.fname.Value, lname = v.names.lname.Value}
			else
				storeTable = {weightData = weightTable, tutorialComplete = tutorialCompleteV, contacts = contactData, phoneNumber = v.phoneNumber.Value, boatData = boatTable, bookmarks = bookmarkData, jail = v.jailTime.Value, driversLicense = v.driversLicense.Value, firearmsLicense = v.firearmsLicense.Value, huntingLicense = v.huntingLicense.Value, userId = v.Name, callsign = v.callsign.Value, criminalRecords = criminalData, carData = cars, gender = v.gender.Value, money = v.money.Value}
			end
			bigTable = storeTable
			--table.insert(bigTable,storeTable)
		end
		--end

		-- PLAYER DATA
		local success, err = pcall(function()
			dataStore:SetAsync(player.UserId,bigTable)
		end)

		if success then
			playerData = true
		end



		-- NOTIFY
		if playerData == true then
			for i,v in pairs(game.Players:GetChildren()) do
				local clone = v.PlayerGui.SystemUI.NotificationBar.CloneMe:Clone()
				clone.Title.Text = "Data Store"
				clone.Desc.Text = "All game data has successfully been saved."
				clone.Time.Value = 5
				clone.Parent = v.PlayerGui.SystemUI.NotificationBar
				clone.Visible = true
			end
		else
			for i,v in pairs(game.Players:GetChildren()) do
				local clone = v.PlayerGui.SystemUI.NotificationBar.CloneMe:Clone()
				clone.Title.Text = "Data Store"
				clone.Desc.Text = "Game data failed to save."
				clone.Time.Value = 5
				clone.Parent = v.PlayerGui.SystemUI.NotificationBar
				clone.Visible = true
			end
			local gameLogs = require(game.ServerStorage.GameLogs)
			gameLogs.makeWebhook("Data Store Failure", "Failed to Save Data Store")
		end
	end
end

function module.saveBusinessData()
	local dataStore = game:GetService("DataStoreService"):GetDataStore("HermitageGameData")
	local bigTable = {}
	for i,v in pairs(game.ServerStorage.Businesses:GetChildren()) do
		local business = v
		local employeesTable = {}
		local businessTable = {}
		local businessInfoTable = {name = v.Name, totalPaycheck = v.TotalPaycheck.Value}

		for l,z in pairs(v.Employees:GetChildren()) do
			local singleTable = {userId = z.Name, pay = z.Value}
			table.insert(employeesTable,singleTable)
		end

		table.insert(businessTable,1,businessInfoTable)
		table.insert(businessTable,employeesTable)
		table.insert(bigTable,businessTable)
	end

	local success, err = pcall(function()
		dataStore:SetAsync("03",bigTable)
	end)
end

function module.Save()
	module.SavePlayerData()
	--module.saveBusinessData()
end

return module
2 Likes

You could make a value “NumberOffPlayers” and then save the amount every few seconds
and then make the value equal to the players.

while true do
NumberOffPlayers = game.Players:GetChildren()
wait(5)
end

and then in another script save the value every 5 seconds or something.

2 Likes

I meant that more as in some people get their data saved and some do not

2 Likes