Database won't save... [Datastore2 Usage, Please Help]

Greetings, I have a problem for so long that my data doesn’t save at all no matter what I tried. As you can see I moved on to Datastore2 since I have heard is much better, but it seems I don’t know why it doesn’t save. The code is down below.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")

local DataStore2 = require(ServerScriptService.DataStore2)

DataStore2.Combine("DATA", "coins", "Server", "Host", "Gamemode", "Difficulty", "Code")

Players.PlayerAdded:Connect(function(player)
	local dataServer = DataStore2("Server", player)
	local dataHost = DataStore2("Host", player)
	local dataGamemode = DataStore2("Gamemode", player)
	local dataDifficulty = DataStore2("Difficulty", player)
	local dataCode = DataStore2("Code", player)
	
	local folder = Instance.new("Folder", player)
	folder.Name = "ServerCreator"
	
	local Server = Instance.new("ObjectValue", folder)
	local Host = Instance.new("ObjectValue", folder)
	local Gamemode = Instance.new("NumberValue", folder)
	local Difficulty = Instance.new("NumberValue", folder)
	local Code = Instance.new("NumberValue", folder)
	Server.Name = "Server"
	Host.Name = "Host"
	Gamemode.Name = "Gamemode"
	Difficulty.Name = "Difficulty"
	Code.Name = "Code"
	
	--LOADING DATA--
	if dataServer:Get() ~= nil then
		Server.value = dataServer:Get()
		print(Server.Value)
	else
		Server.Value = "None"
	end
	if dataHost:Get() ~= nil then
		Host.value = dataHost:Get()
		print(Host.Value)
	else
		Host.Value = "None"
	end
	if dataGamemode:Get() ~= nil then
		Gamemode.value = dataGamemode:Get()
		print(Gamemode.Value)
	else
		Gamemode.Value = 0
	end
	if dataDifficulty:Get() ~= nil then
		Difficulty.value = dataDifficulty:Get()
		print(Difficulty.Value)
	else
		Difficulty.Value = 0
	end
	if dataCode:Get() ~= nil then
		Code.value = dataCode:Get()
		print(Code.Value)
	else
		Code.Value = 0
	end
	
	--SAVING DATA--
	Server.Changed:Connect(function()
		dataServer:Set(Server.Value)
	end)
	Host.Changed:Connect(function()
		dataHost:Set(Host.Value)
	end)
	Gamemode.Changed:Connect(function()
		dataGamemode:Set(Gamemode.Value)
	end)
	Difficulty.Changed:Connect(function()
		dataDifficulty:Set(Difficulty.Value)
	end)
	Code.Changed:Connect(function()
		dataCode:Set(Code.Value)
	end)
end)

Please help me fix it! Thanks.

Did you create a SaveInStudio value for DataStore2?

No, I didn’t… I also tried in the actual game, not just in the studio and it didn’t work. But, how do I make it anyways? In my game, I have an Admin Panel that I use to show every data and player bans, etc. The data wouldn’t load in the game.

Make a SaveInStudio bool value in ServerStorage and set it to true/false if you want it to save in studio or not. DataStore2 will not save without this value existing.

Do I have to connect this SaveInStudio value with a script or I just let it there?

Just leave it there, it will work.

It didn’t work (character limit)

Are you in studio? If you are and the value is set to false it won’t save. Please show me any error messages if you have some.

I was in the actual game, not in the studio.

EDIT: I went in the studio and the value was set to true but it didn’t work.

Datastore2 also uses roblox’s datastore, so roblox wont save OBJECTs in their database.
Only strings, tables and numbers

I also have numbers not just objects. Then how can I save the host’s name and party name (since I am trying to do a party system). I transfer the data to a new place that has space for players to join in.

Because maybe instances are creating error and wont let numbers save in database?
Maybe try storing the Host name instead of their Instance.

local hostVal = Instance.new("StringValue")
hostVal.Value = player.Name

then save the value

Well, thanks, this solved the Host and Servervalues, but none of them still save.

I am not a very good scripter yet but try putting,

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

this at the end of your code.

It didn’t work (character limit)

1 Like

Well I’ve never seen someone using your getting data method before.
Why not use pcall to catch the errors?

--LOADING DATA--
local serverData,hostData,GamemodeData,difficultyData,dataCodeData
local success,errorr = pcall(function()
	serverData = dataServer:Get()
	hostData = dataHost:Get()
	GamemodeData = dataGamemode:Get()
	difficultyData = dataDifficulty:Get()
	dataCodeData = dataCode:Get()
end)


if success then
	Server.Value = serverData
	Host.Value = hostData
	Gamemode.Value = GamemodeData
	Difficulty.Value = difficultyData
	Code.Value = dataCodeData
elseif errorr then
	Server.Value = "None"
	Host.Value = "None"
	Gamemode.Value = 0
	Difficulty.Value = 0
	Code.Value = 0
	error(errorr)
end

Screenshot 2021-12-30 114134
Screenshot 2021-12-30 114152

Fixed Code
--LOADING DATA--
local serverData,hostData,GamemodeData,difficultyData,dataCodeData
local success,errorr = pcall(function()
	serverData = dataServer:Get()
	hostData = dataHost:Get()
	GamemodeData = dataGamemode:Get()
	difficultyData = dataDifficulty:Get()
	dataCodeData = dataCode:Get()
end)


if success then
	if serverData and hostData and GamemodeData and difficultyData and dataCodeData then
		Server.Value = serverData
		Host.Value = hostData
		Gamemode.Value = GamemodeData
		Difficulty.Value = difficultyData
		Code.Value = dataCodeData
	else
		Server.Value = "None"
		Host.Value = "None"
		Gamemode.Value = 0
		Difficulty.Value = 0
		Code.Value = 0
	end
elseif errorr then
	error(errorr)
end

Hmm so its a problem with saving… let’s see :thinking:

Saving part code, added pcall function to check errors.
--SAVING DATA--
local yes,no = pcall(function()
	Server.Changed:Connect(function()
		dataServer:Set(Server.Value)
	end)
	Host.Changed:Connect(function()
		dataHost:Set(Host.Value)
	end)
	Gamemode.Changed:Connect(function()
		dataGamemode:Set(Gamemode.Value)
	end)
	Difficulty.Changed:Connect(function()
		dataDifficulty:Set(Difficulty.Value)
	end)
	Code.Changed:Connect(function()
		dataCode:Set(Code.Value)
	end)
end)

if yes then
	print("Saved data for "..plr.Name)
elseif no then
	error(no)
end

I am back and I tried it, sadly it doesn’t work.

I am not good with DataStore2, and since you are not storing any Tables value.
Why not use roblox’s normal datastore with :SetAsync?

People says setAsync makes you lose data but only for tables not for single values.
I also use roblox’s datastore but since i have tables, I use :UpdateAsync and haven’t got any data lose reports.