Datastore does not save

Hi,
I have a problem with my DataStore. The DataStore saves the values ​​"money" and “bank”, but not the other values. All values ​​except “Money” and “Bank” are StringValues. There are no error messages in the output
Can you help me with my problem?

My ServerScript:

local DataSoreService = game:GetService("DataStoreService")
local PersoStore = DataSoreService:GetDataStore("PersoStore")

local function onPlayerJoin(plr)
	local stats2 = Instance.new("IntValue",plr)
	stats2.Name = "Values"	
	local Briefe = Instance.new("NumberValue",stats2)
	Briefe.Name = "Briefe"
	
	local Bank = Instance.new("IntValue",stats2)
	Bank.Name = "Bank"
	local Geld = Instance.new("IntValue",stats2)
	Geld.Name = "Geld"
	
	local Geschlecht = Instance.new("StringValue",stats2)
	Geschlecht.Name = "Geschlecht"
	local Vorname = Instance.new("StringValue", stats2)
	Vorname.Name = "Vorname"
	local Nachname = Instance.new("StringValue", stats2)
	Nachname.Name = "Nachname"
	local Nationalitat = Instance.new("StringValue", stats2)
	Nationalitat.Name = "Nationalitat"
	Nationalitat.Value = "Deutsch"
	local Augenfarbe = Instance.new("StringValue", stats2)
	Augenfarbe.Name = "Augenfarbe"
	local Groesse = Instance.new("StringValue", stats2)
	Groesse.Name = "Groesse"
	local Datum = Instance.new("StringValue", stats2)
	Datum.Name = "Datum"
	local Geburtsort = Instance.new("StringValue", stats2)
	Geburtsort.Name = "Geburtsort"
	
	local playerUserId = "Player_"..plr.UserId
	local data = PersoStore:GetAsync(playerUserId)
	if data then
		Bank.Value = data["Bank"]
		Geld.Value = data["Geld"]
		Geschlecht.Value = data["Geschlecht"]
		Vorname.Value = data["Vorname"]
		Nachname.Value = data["Nachname"]
		Nationalitat.Value = data["Nationalitat"]
		Augenfarbe.Value = data["Augenfarbe"]
		Groesse.Value = data["Groesse"]
		Datum.Value = data["Datum"]
		Geburtsort.Value = data["Geburtsort"]
	else
		Bank.Value = 0
		Geld.Value = 5000
	end
end

local function create_table(plr)
	local player_values = {}
	for _, stat in pairs(plr.Values:GetChildren()) do
		player_values[stat.Name] = stat.Value
	end
	return player_values
end

local function onPlayerExit(plr)
	local player_values = create_table(plr)
	local sucess, err = pcall(function()
		local playerUserId = 'Player_'..plr.UserId
		PersoStore:SetAsync(playerUserId, player_values)
	end)
	if not sucess then
		warn("Daten konnten nicht gespeichert werden")
	end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

Thanks for helping
Pino

How are you checking that the data is not saving? If Nationalitat’s is an empty string, then yes, Nationalitat is not being saved.

If Nationalitat’s value is Dutch, then it’s loading right. The issue might be that you’re not changing values correctly ingame, so the datastore is saving the wrong data in this case

I check whether it saves with not sucess in the function onPlayerExit (plr). The value “Nationality” saves correctly. So the error lies in the onPlayerExit function. I had the values ​​output in the OnPlayerJoin function. All except “Nationality” are empty. Do you have any idea why that could be? Is something wrong in the table?

My Prints:

local data = PersoStore:GetAsync(playerUserId)
	if data then
		print("loading")
		Geschlecht.Value = data["Geschlecht"]
		print(Geschlecht.Value)
		Vorname.Value = data["Vorname"]
		print(Vorname.Value)
		Nachname.Value = data["Nachname"]
		print(Nachname.Value)
		Nationalitat.Value = data["Nationalitat"]
		print(Nationalitat.Value)
		Augenfarbe.Value = data["Augenfarbe"]
		print(Augenfarbe.Value)
		Groesse.Value = data["Groesse"]
		print(Groesse.Value)
		Datum.Value = data["Datum"]
		print(Datum.Value)
		Geburtsort.Value = data["Geburtsort"]
		print(Geburtsort.Value)
	end

The OnPlayerExit function:

local function onPlayerExit(plr)
	local player_values = create_table(plr)
	local sucess, err = pcall(function()
		local playerUserId = 'Player_'..plr.UserId
		PersoStore:SetAsync(playerUserId, player_values)
		print("Daten gespeichert")
	end)
	if not sucess then
		warn("Daten konnten nicht gespeichert werden")
	end
end

Does anyone else have a suggestion for a solution?

1 Like

If you’re testing this in studio, when you press stop, there’s a big chance that the PlayerRemoving function doesn’t have enough time to process before the server shuts down, so you should add a BindToClose function like this:

game:BindToClose:Connect(function()
	for _,plr in pairs(game.Players:GetPlayers()) do
		onPlayerExit(plr)
	end
end)

Thank you for trying to help me.
Unfortunately that didn’t help either. By the way, after BintToClose you don’t need the: Connect, because you can script it as follows.

game:BindToClose(function()
	for _,plr in pairs(game.Players:GetPlayers()) do
		onPlayerExit(plr)
	end
end)