I desperate with the code

I need your help I’m trying to save a string value when a player leaves the game and when he re-enters it fills the problem is that once in the case of the player the value was saved it is not output as nil but as nothing. as if he would save the nothing although it is a list with the syntax playerName,playerName, … is. I don’t see a solution anymore. Please I need your help and tricks and ideas.

ocal DataStore = game:GetService("DataStoreService"):GetDataStore("MyDataStore")

game.Players.PlayerAdded:Connect(function(plr)
	
	-- Beginn der Variablen erstellung (Hier wird alles von neuem erstellt)
	
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = plr
	
	local folderhandy = Instance.new("Folder")
	folderhandy.Name = "Handysavings"
	folderhandy.Parent = plr
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = folder
	
	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = folder
	
	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Parent = folder

	local exp = Instance.new("IntValue")
	exp.Name = "Exp"
	exp.Parent = folder

	local maxExp = Instance.new("IntValue")
	maxExp.Name = "Max"
	maxExp.Parent = folder
	maxExp.Value = 250
	
	--local bs1 = Instance.new("IntValue")
	--bs1.Name = "Value"
	--local scriptos1 = plr.PlayerGui:WaitForChild("BestimmungderKostenderAutos")
	--bs1.Parent = scriptos1
	
	--local bs2 = Instance.new("IntValue")
	--bs2.Name = "Corst2"
	--local scriptos2 = plr.PlayerGui.CSG.Frame.Car1
	--bs2.Parent = scriptos2
	
	--local bs3 = Instance.new("IntValue")
	--bs3.Name = "Corst3"
	--local scriptos3 = plr.PlayerGui.CSG.Frame.Car2
	--bs3.Parent = scriptos3
	
	--local bs4 = Instance.new("IntValue")
	--bs4.Name = "Corst4"
	--local scriptos4 = plr.PlayerGui.CSG.Frame.Car3
	--bs4.Parent = scriptos4
	
	--local bs5 = Instance.new("IntValue")
	--bs5.Name = "Corst"
	--local scriptos5 = plr.PlayerGui.CSG.Frame.TextButton
	--bs5.Parent = scriptos5
	
	local kontaktliste = Instance.new("StringValue")
	kontaktliste.Name = "kontaktliste"
	kontaktliste.Parent = folderhandy
	
	local kontaktticker = Instance.new("IntValue")
	kontaktticker.Name = "kontaktticker"
	kontaktticker.Parent = folderhandy
	
	

	exp.Changed:Connect(function(val)
		if exp.Value >= maxExp.Value then
			-- Do stuff.

			level.Value = level.Value + 1
			exp.Value = 0
			maxExp.Value = maxExp.Value * 1.12
		end
	end)
	
	-- hier werden leblose variablen erstellt
	
	local cashStored
	local killsStored
	local levelStored
	local expStored	
	local maxStored
	--local bostStored
	--local bost2Stored
	--local bost3Stored
	--local bost4Stored
	--local bost5Stored
	local kontakteStored
	local kontakttickerStored
	
	-- Hier gibt man den leblosen Variablen einen Sinn (die werden mit daten gefüllt aus dem datastore)
	
	local success, errorMessage = pcall(function()
		cashStored = DataStore:GetAsync(plr.UserId.."cash")
		killsStored = DataStore:GetAsync(plr.UserId.."kills")
		levelStored = DataStore:GetAsync(plr.UserId.."level")
		expStored = DataStore:GetAsync(plr.UserId.."exp")
		maxStored = DataStore:GetAsync(plr.UserId.."maxExp")
		--bostStored = DataStore:GetAsync(plr.UserId.."bs1")
		--bost2Stored = DataStore:GetAsync(plr.UserId.."bs2")
		--bost3Stored = DataStore:GetAsync(plr.UserId.."bs3")
		--bost4Stored = DataStore:GetAsync(plr.UserId.."bs4")
		--bost5Stored = DataStore:GetAsync(plr.UserId.."bs5")
		kontakteStored = DataStore:GetAsync(plr.UserId.."kontaktliste")
		kontakttickerStored = DataStore:GetAsync(plr.UserId.."kontaktticker")
	end)
	if not success then
		warn("Error loading 'kontaktliste' data: " .. errorMessage)
	end
	
	-- Hier gibt man den erstellten Variablen ihre Werte
	
	if cashStored ~= nil then
		cash.Value = cashStored
	else
		cash.Value = 0
	end
	
	if killsStored ~= nil then
		kills.Value = killsStored
	else
		kills.Value = 0
	end
	
	if levelStored ~= nil then
		level.Value = levelStored
	else
		level.Value = 1
	end
	
	if expStored ~= nil then
		exp.Value = expStored
	else
		exp.Value = 0
	end
	
	if maxStored ~= nil then
		maxExp.Value = maxStored
	else
		maxExp.Value = 250
	end
	
	--if bostStored ~= nil then
		--bs1.Value = bostStored
	--else
		--bs1.Value = 100
	--end
	
	--if bost2Stored ~= nil then
		--bs2.Value = bost2Stored
	--else
		--bs2.Value = 25
	--end
	
	--if bost3Stored ~= nil then
		--bs3.Value = bost3Stored
	--else
		--bs3.Value = 25
	--end
	
	--if bost4Stored ~= nil then
		--bs4.Value = bost4Stored
	--else
		--bs4.Value = 50
	--end
	
	--if bost5Stored ~= nil then
		--bs5.Value = bost5Stored
	--else
		--bs5.Value = 50
	--end
	
	if  kontakteStored ~= nil then
		kontaktliste.Value = kontakteStored
		print(kontakteStored)

	else
		kontaktliste.Value = ""
		print("666666666666666666666666666666666666666666666666")
		print(kontakteStored)
	end
	
	local ordner = plr.PlayerGui.Handy.Frame.kontakteframe.Edit.Kontakte
	print("1111111111111111111111111111111111111111")
	

	local kontakte = {}  -- Eine Tabelle, um die Kontakte zu speichern
	print("2222222222222222222222222222222222222222222222222222222")

		-- Trenne die Kontaktliste in einzelne Namen (angenommen, sie sind durch Kommas getrennt)
	for kontaktName in kontaktliste.Value:gmatch("[^,]+") do
		print("333333333333333333333333333333333333333333333333333")
		table.insert(kontakte, kontaktName)
		print("4444444444444444444444444444444444444444444444444444444444444444444444")
	end
	print("5555555555555555555555555555555555555555555555555555555555")

		-- Lösche vorhandene StringValues im Ordner, um Konflikte zu vermeiden
	for _, child in pairs(ordner:GetChildren()) do
		print("77777777777777777777777777777777777777777777777777777777777777777777")
		if child:IsA("StringValue") then
			print("888888888888888888888888888888888888888888888888888")
			child:Destroy()
			print("99999999999999999999999999999999999999999999999999999999999999")
		end
		print("10101010010101001010101010010101001010101001")
	end
	print("11 11 11 11 11 11 1111111111111111111111111111")
	

		-- Erstelle für jeden Kontakt eine separate StringValue und lege sie im Ordner ab
	for _, kontaktNameh in ipairs(kontakte) do
		print("1212121212121212121212121212121212121212121")
		print("Anzahl der Kontakte:", #kontakte)
		print("fehler1")
		local kontaktValue = Instance.new("StringValue")
		print("fehler2")
		kontaktValue.Name = kontaktNameh.Name
		print("fehler3")
		kontaktValue.Value = kontaktNameh.Name
		print("fehler4")
		kontaktValue.Parent = ordner
		print("fehler5")
	end
	print("fehler6")
	
	
	if kontakttickerStored ~= nil then
		kontaktticker.Value = kontakttickerStored
		-- das muss noch in den edit rein
	else
		kontaktticker.Value = 0
	end
	
	
end)

-- Hier werden die Variablen gespeichert

game.Players.PlayerRemoving:Connect(function(plr)
	
	
	local success, errorMessage = pcall(function()
		--local scriptos1 = plr.PlayerGui:WaitForChild("BestimmungderKostenderAutos")
		--local valuchen = scriptos1:WaitForChild("Value")
		DataStore:SetAsync(plr.UserId.."cash",plr.leaderstats.Cash.Value)
		DataStore:SetAsync(plr.UserId.."kills",plr.leaderstats.Kills.Value)
		DataStore:SetAsync(plr.UserId.."level",plr.leaderstats.Level.Value)
		DataStore:SetAsync(plr.UserId.."exp",plr.leaderstats.Exp.Value)
		DataStore:SetAsync(plr.UserId.."maxExp",plr.leaderstats.Max.Value)
		DataStore:SetAsync(plr.UserId.."kontaktliste",plr.Handysavings.kontaktliste.Value)
		DataStore:SetAsync(plr.UserId.."kontaktticker",plr.Handysavings.kontaktticker.Value)
		--DataStore:SetAsync(plr.UserId.."bs1",valuchen.Value)
		--DataStore:SetAsync(plr.UserId.."bs2",plr.PlayerGui.CSG.Frame.Car1.Corst2.Value)
		--DataStore:SetAsync(plr.UserId.."bs3",plr.PlayerGui.CSG.Frame.Car2.Corst3.Value)
		--DataStore:SetAsync(plr.UserId.."bs4",plr.PlayerGui.CSG.Frame.Car3.Corst4.Value)
		--DataStore:SetAsync(plr.UserId.."bs5",plr.PlayerGui.CSG.Frame.TextButton.Corst.Value)
	end)
	
	if not success then
		warn("Error saving 'kontaktliste' data: " .. errorMessage)
	end
	
end)

Make sure to save the data when the server closes as well.

function savedata(plr)
	local success, errorMessage = pcall(function()
		--local scriptos1 = plr.PlayerGui:WaitForChild("BestimmungderKostenderAutos")
		--local valuchen = scriptos1:WaitForChild("Value")
		DataStore:SetAsync(plr.UserId.."cash",plr.leaderstats.Cash.Value)
		DataStore:SetAsync(plr.UserId.."kills",plr.leaderstats.Kills.Value)
		DataStore:SetAsync(plr.UserId.."level",plr.leaderstats.Level.Value)
		DataStore:SetAsync(plr.UserId.."exp",plr.leaderstats.Exp.Value)
		DataStore:SetAsync(plr.UserId.."maxExp",plr.leaderstats.Max.Value)
		DataStore:SetAsync(plr.UserId.."kontaktliste",plr.Handysavings.kontaktliste.Value)
		DataStore:SetAsync(plr.UserId.."kontaktticker",plr.Handysavings.kontaktticker.Value)
		--DataStore:SetAsync(plr.UserId.."bs1",valuchen.Value)
		--DataStore:SetAsync(plr.UserId.."bs2",plr.PlayerGui.CSG.Frame.Car1.Corst2.Value)
		--DataStore:SetAsync(plr.UserId.."bs3",plr.PlayerGui.CSG.Frame.Car2.Corst3.Value)
		--DataStore:SetAsync(plr.UserId.."bs4",plr.PlayerGui.CSG.Frame.Car3.Corst4.Value)
		--DataStore:SetAsync(plr.UserId.."bs5",plr.PlayerGui.CSG.Frame.TextButton.Corst.Value)
	end)
	
	if not success then
		warn("Error saving 'kontaktliste' data: " .. errorMessage)
	end
end

game.Players.PlayerRemoving:Connect(savedata)

game:BindToClose(function()
	for i,v in pairs(game.Players:GetPlayers()) do 
		savedata(v)
	end
end)

You probably didn’t test your code in the actual game, where it would run PlayerRemoving before shutting the server down (most of the time), but in Studio, it closes the server before running PlayerRemoving.

Regrettably, still nothing is spent.