How to make a database?

Well, I want to make a database that loads text data and the like for a settings menu. In fact I already have a “database” but it is faulty since sometimes it does not load the data and then does not want to save it.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local Lighting = game:GetService("Lighting")

local Confg = ReplicatedStorage:WaitForChild("ConfiguracionGeneral"):WaitForChild("JuegoConfg")
local FpsFPSData = DataStoreService:GetDataStore("FpsDataView")
local PingDataStore = DataStoreService:GetDataStore("PingService")
local BrilloService = DataStoreService:GetDataStore("BrilloService")
local DropPlayerSelectionService = DataStoreService:GetDataStore("DropPlayerSelectionService")
--FpsGui
local ViewFPS = {
	Enabled = function(player: Player)
		local Main = player:WaitForChild("PlayerGui"):WaitForChild("Configuracion"):WaitForChild("Ajustes"):WaitForChild("Juego"):WaitForChild("ScrollingJuego")
		Main:WaitForChild("MostrarFps"):WaitForChild("Abrir").Icon.Position = UDim2.new(0.85, 0, 0.5, 0)
		Main:WaitForChild("MostrarFps"):WaitForChild("Abrir").BackgroundColor3 = Color3.new(0.317647, 0.541176, 0.313725)
		Main:WaitForChild("MostrarFps"):WaitForChild("Abrir").Icon.BackgroundColor3 = Color3.new(0.407843, 0.701961, 0.317647)
		Main:WaitForChild("MostrarFps"):WaitForChild("Abrir").Icon.ImageColor3 = Color3.new(0.501961, 1, 0.501961)
		Main:WaitForChild("MostrarFps"):WaitForChild("Abrir").Icon.Image = "http://www.roblox.com/asset/?id=6652973166"
		Main:WaitForChild("MostrarFps"):WaitForChild("Abrir"):WaitForChild("Take").Value = "False"
		player:WaitForChild("PlayerGui"):WaitForChild("MenuDeMapa"):WaitForChild("Rendimiento"):WaitForChild("FPS").Visible = true
	end,

	UnEnabled = function(player: Player)
		local Main = player:WaitForChild("PlayerGui"):WaitForChild("Configuracion"):WaitForChild("Ajustes"):WaitForChild("Juego"):WaitForChild("ScrollingJuego")
		Main:WaitForChild("MostrarFps"):WaitForChild("Abrir").Icon.Position = UDim2.new(0.15, 0, 0.5, 0)
		Main:WaitForChild("MostrarFps"):WaitForChild("Abrir").BackgroundColor3 =  Color3.new(0.529412, 0.313725, 0.313725)
		Main:WaitForChild("MostrarFps"):WaitForChild("Abrir").Icon.BackgroundColor3 =  Color3.new(0.509804, 0.509804, 0.509804)
		Main:WaitForChild("MostrarFps"):WaitForChild("Abrir").Icon.ImageColor3 = Color3.new(1, 0.537255, 0.537255)
		Main:WaitForChild("MostrarFps"):WaitForChild("Abrir").Icon.Image = "http://www.roblox.com/asset/?id=6652972833"
		Main:WaitForChild("MostrarFps"):WaitForChild("Abrir"):WaitForChild("Take").Value = "True"
		player:WaitForChild("PlayerGui"):WaitForChild("MenuDeMapa"):WaitForChild("Rendimiento"):WaitForChild("FPS").Visible = false
	end,
}
--PingGui
local ViewPing = {
	EnabledPing = function(player: Player)
		local Main = player:WaitForChild("PlayerGui"):WaitForChild("Configuracion"):WaitForChild("Ajustes"):WaitForChild("Juego"):WaitForChild("ScrollingJuego")
		Main:WaitForChild("MostrarPing"):WaitForChild("Abrir").Icon:TweenPosition(UDim2.new(0.85, 0, 0.5, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0, true)
		Main:WaitForChild("MostrarPing").Abrir.BackgroundColor3 = Color3.new(0.317647, 0.541176, 0.313725)
		Main:WaitForChild("MostrarPing"):WaitForChild("Abrir").Icon.BackgroundColor3 = Color3.new(0.407843, 0.701961, 0.317647)
		Main:WaitForChild("MostrarPing"):WaitForChild("Abrir").Icon.ImageColor3 = Color3.new(0.501961, 1, 0.501961)
		Main:WaitForChild("MostrarPing"):WaitForChild("Abrir").Icon.Image = "http://www.roblox.com/asset/?id=6652973166"
		Main:WaitForChild("MostrarPing"):WaitForChild("Abrir"):WaitForChild("Take").Value = "False"
		player:WaitForChild("PlayerGui"):WaitForChild("MenuDeMapa"):WaitForChild("Rendimiento"):WaitForChild("Ping").Visible = true
	end,

	UnEnabledPing = function(player: Player)
		local Main = player:WaitForChild("PlayerGui"):WaitForChild("Configuracion"):WaitForChild("Ajustes"):WaitForChild("Juego"):WaitForChild("ScrollingJuego")
		Main:WaitForChild("MostrarPing"):WaitForChild("Abrir").Icon:TweenPosition(UDim2.new(0.15, 0, 0.5, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0, true)
		Main:WaitForChild("MostrarPing").Abrir.BackgroundColor3 =  Color3.new(0.529412, 0.313725, 0.313725)
		Main:WaitForChild("MostrarPing"):WaitForChild("Abrir").Icon.BackgroundColor3 =  Color3.new(0.509804, 0.509804, 0.509804)
		Main:WaitForChild("MostrarPing"):WaitForChild("Abrir").Icon.ImageColor3 = Color3.new(1, 0.537255, 0.537255)
		Main:WaitForChild("MostrarPing"):WaitForChild("Abrir").Icon.Image = "http://www.roblox.com/asset/?id=6652972833"
		Main:WaitForChild("MostrarPing"):WaitForChild("Abrir"):WaitForChild("Take").Value = "True"
		player:WaitForChild("PlayerGui"):WaitForChild("MenuDeMapa"):WaitForChild("Rendimiento"):WaitForChild("Ping").Visible = false
	end,
}
--Brillo
local VolumenDeBrillo = {

	BrilloVolumen1 = function(player: Player)
		local Main = player:WaitForChild("PlayerGui"):WaitForChild("Configuracion"):WaitForChild("Ajustes"):WaitForChild("Juego"):WaitForChild("ScrollingJuego")
		local infor = Main:WaitForChild("Brillo")
		infor:WaitForChild("CantidadDeBrillo").Text = "0.1"
		infor:WaitForChild("Marco"):WaitForChild("Nivel"):TweenPosition(UDim2.new(-0.4, 0, 0, 0), nil, nil, 0)
	end,

	BrilloVolumen2 = function(player: Player)
		local Main = player:WaitForChild("PlayerGui"):WaitForChild("Configuracion"):WaitForChild("Ajustes"):WaitForChild("Juego"):WaitForChild("ScrollingJuego")
		local infor = Main:WaitForChild("Brillo")
		infor:WaitForChild("CantidadDeBrillo").Text = "0.2"
		infor:WaitForChild("Marco"):WaitForChild("Nivel"):TweenPosition(UDim2.new(-0.3, 0, 0, 0), nil, nil, 0)
	end,

	BrilloVolumen3 = function(player: Player)
		local Main = player:WaitForChild("PlayerGui"):WaitForChild("Configuracion"):WaitForChild("Ajustes"):WaitForChild("Juego"):WaitForChild("ScrollingJuego")
		local infor = Main:WaitForChild("Brillo")
		infor:WaitForChild("CantidadDeBrillo").Text = "0.3"
		infor:WaitForChild("Marco"):WaitForChild("Nivel"):TweenPosition(UDim2.new(-0.2, 0, 0, 0), nil, nil, 0)
	end,

	BrilloVolumen4 = function(player: Player)
		local Main = player:WaitForChild("PlayerGui"):WaitForChild("Configuracion"):WaitForChild("Ajustes"):WaitForChild("Juego"):WaitForChild("ScrollingJuego")
		local infor = Main:WaitForChild("Brillo")
		infor:WaitForChild("CantidadDeBrillo").Text = "0.4"
		infor:WaitForChild("Marco"):WaitForChild("Nivel"):TweenPosition(UDim2.new(-0.1, 0, 0, 0), nil, nil, 0)
	end,

	BrilloVolumen5 = function(player: Player)
		local Main = player:WaitForChild("PlayerGui"):WaitForChild("Configuracion"):WaitForChild("Ajustes"):WaitForChild("Juego"):WaitForChild("ScrollingJuego")
		local infor = Main:WaitForChild("Brillo")
		infor:WaitForChild("CantidadDeBrillo").Text = "0.5"
		infor:WaitForChild("Marco"):WaitForChild("Nivel"):TweenPosition(UDim2.new(0.0, 0, 0, 0), nil, nil, 0)
	end,

	BrilloVolumen6 = function(player: Player)
		local Main = player:WaitForChild("PlayerGui"):WaitForChild("Configuracion"):WaitForChild("Ajustes"):WaitForChild("Juego"):WaitForChild("ScrollingJuego")
		local infor = Main:WaitForChild("Brillo")
		infor:WaitForChild("CantidadDeBrillo").Text = "0.6"
		infor:WaitForChild("Marco"):WaitForChild("Nivel"):TweenPosition(UDim2.new(0.1, 0, 0, 0), nil, nil, 0)
	end,

	BrilloVolumen7 = function(player: Player)
		local Main = player:WaitForChild("PlayerGui"):WaitForChild("Configuracion"):WaitForChild("Ajustes"):WaitForChild("Juego"):WaitForChild("ScrollingJuego")
		local infor = Main:WaitForChild("Brillo")
		infor:WaitForChild("CantidadDeBrillo").Text = "0.7"
		infor:WaitForChild("Marco"):WaitForChild("Nivel"):TweenPosition(UDim2.new(0.2, 0, 0, 0), nil, nil, 0)
	end,

	BrilloVolumen8 = function(player: Player)
		local Main = player:WaitForChild("PlayerGui"):WaitForChild("Configuracion"):WaitForChild("Ajustes"):WaitForChild("Juego"):WaitForChild("ScrollingJuego")
		local infor = Main:WaitForChild("Brillo")
		infor:WaitForChild("CantidadDeBrillo").Text = "0.8"
		infor:WaitForChild("Marco"):WaitForChild("Nivel"):TweenPosition(UDim2.new(0.3, 0, 0, 0), nil, nil, 0)
	end,

	BrilloVolumen9 = function(player: Player)
		local Main = player:WaitForChild("PlayerGui"):WaitForChild("Configuracion"):WaitForChild("Ajustes"):WaitForChild("Juego"):WaitForChild("ScrollingJuego")
		local infor = Main:WaitForChild("Brillo")
		infor:WaitForChild("CantidadDeBrillo").Text = "0.9"
		infor:WaitForChild("Marco"):WaitForChild("Nivel"):TweenPosition(UDim2.new(0.4, 0, 0, 0), nil, nil, 0)
	end,

	BrilloVolumen10 = function(player: Player)
		local Main = player:WaitForChild("PlayerGui"):WaitForChild("Configuracion"):WaitForChild("Ajustes"):WaitForChild("Juego"):WaitForChild("ScrollingJuego")
		local infor = Main:WaitForChild("Brillo")
		infor:WaitForChild("CantidadDeBrillo").Text = "1.0"
		infor:WaitForChild("Marco"):WaitForChild("Nivel"):TweenPosition(UDim2.new(0.485, 0, 0, 0), nil, nil, 0)
	end,
}

--PingGui
local DropMaterialPlayerSelection = {
	ForAll = function(player: Player)
		local Main = player:WaitForChild("PlayerGui"):WaitForChild("Configuracion"):WaitForChild("Ajustes"):WaitForChild("Juego"):WaitForChild("ScrollingJuego")
	end,

	ForLocalPlayer = function(player: Player)
		local Main = player:WaitForChild("PlayerGui"):WaitForChild("Configuracion"):WaitForChild("Ajustes"):WaitForChild("Juego"):WaitForChild("ScrollingJuego")
	end,

	ForChosenFriends = function(player: Player)
		local Main = player:WaitForChild("PlayerGui"):WaitForChild("Configuracion"):WaitForChild("Ajustes"):WaitForChild("Juego"):WaitForChild("ScrollingJuego")
	end,
}

game.Players.PlayerAdded:Connect(function(player: Player)
	local graficosFol = Instance.new("Folder")
	graficosFol.Name = "JuegoConfg"
	graficosFol.Parent = player

	local graphicsBools = {
		{type = "StringValue", name = "FPS"},
		{type = "StringValue", name = "Ping"},
		{type = "StringValue", name = "BrilloVol"},
		{type = "StringValue", name = "DropSelectionPlayer"},
		-- Add more values for your graphics folder!
		--{tipo = "String Value", nombre = "Nombre de la configuración"},
	}

	for _, value in pairs(graphicsBools) do
		local val = Instance.new(value.type)
		val.Name = value.name
		val.Parent = graficosFol
	end

	--FPS
	local FPSData
	local succes: boolean, errormessage: string = pcall(function()
		FPSData = FpsFPSData:GetAsync(player.UserId.."Value")
		---Ping
		local PingData
		local succes: boolean, errormessage: string = pcall(function()
			PingData = PingDataStore:GetAsync(player.UserId.."Value")
			--Brillo
			local BrilloData
			local succes: boolean, errormessage: string = pcall(function()
				BrilloData = BrilloService:GetAsync(player.UserId.."Value")
				--Brillo
				local DropMaterial
				local succes: boolean, errormessage: string = pcall(function()
					DropMaterial = DropPlayerSelectionService:GetAsync(player.UserId.."Value")
				end)

				if (succes) then

					-- FPS
					if (FPSData) then
						player:WaitForChild("JuegoConfg"):FindFirstChild("FPS").Value = FPSData.fps
						ViewFPS[FPSData.fps](player)
					else
						player:WaitForChild("JuegoConfg"):FindFirstChild("FPS").Value = "UnEnabled"
						ViewFPS.UnEnabled(player)
					end
					--Ping
					if (PingData) then
						player:WaitForChild("JuegoConfg"):FindFirstChild("Ping").Value = PingData.ping
						ViewPing[PingData.ping](player)
					else
						player:WaitForChild("JuegoConfg"):FindFirstChild("Ping").Value = "UnEnabledPing"
						ViewPing.UnEnabledPing(player)
					end
					--brillo
					if (BrilloData) then
						player:WaitForChild("JuegoConfg"):FindFirstChild("BrilloVol").Value = BrilloData.brillo
						VolumenDeBrillo[BrilloData.brillo](player)
					else
						player:WaitForChild("JuegoConfg"):FindFirstChild("BrilloVol").Value = "BrilloVolumen5"
						VolumenDeBrillo.BrilloVolumen5(player)
					end
					--DropMaterial
					if (DropMaterial) then
						player:WaitForChild("JuegoConfg"):FindFirstChild("DropSelectionPlayer").Value = DropMaterial.choseplayers
						DropMaterialPlayerSelection[DropMaterial.choseplayers](player)
					else
						player:WaitForChild("JuegoConfg"):FindFirstChild("DropSelectionPlayer").Value = "ForLocalPlayer"
						DropMaterialPlayerSelection.ForLocalPlayer(player)
					end
				else
					warn(errormessage)
				end
			end)
		end)
	end)
end)

--Sombras
Confg.ChangeConfg.OnServerEvent:Connect(function(player: Player, setting: string, value: string)

	if (not player.JuegoConfg:FindFirstChild(setting)) then return end
	if (not ViewFPS[value]) then return end
	
	player.JuegoConfg[setting].Value = value
	ViewFPS[value](player)

	if (not player.JuegoConfg:FindFirstChild(setting)) then return end
	if (not ViewPing[value]) then return end

	player.JuegoConfg[setting].Value = value
	ViewPing[value](player)

	if (not player.JuegoConfg:FindFirstChild(setting)) then return end
	if (not VolumenDeBrillo[value]) then return end
	
	player.JuegoConfg[setting].Value = value
	VolumenDeBrillo[value](player)
end)

game.Players.PlayerRemoving:Connect(function(player: Player)
	local values = {
		-- Get all the values!
		fps = player.JuegoConfg.FPS.Value,
		ping = player.JuegoConfg.Ping.Value,
		brillo = player.JuegoConfg.BrilloVol.Value,
	}

	local success: boolean, errormessage: string = pcall(function()
		FpsFPSData:SetAsync(player.UserId .. "Value", values)
		PingDataStore:SetAsync(player.UserId .. "Value", values)
		BrilloService:SetAsync(player.UserId .. "Value", values)
	end)

	if (success) then
		print("Se ha guardado correctamente")
	else
		print("hubo un error al guardar")
		warn(errormessage)
	end
end)

You’d want to use game:BindToClose() to save data secondarily - hence, you’d need to create a function, and spread it with the Players.PlayerRemoving event and game:BindToClose.

In some cases, the server shutdowns before PlayerRemoving is fired, so that’d be the cause of losing data.

1 Like