Datastore dont datastoring

local Players = game.Players
local DataStoreService = game:GetService("DataStoreService")

local Info1 = DataStoreService:GetDataStore("Info1")

local function savePlayerData(plr)
	local success, error = pcall(function()
		local PlayerGui = plr.PlayerGui
		for _, button in pairs(plr.PlayerGui:WaitForChild("Game").Profit.Markets:GetChildren()) do 
			if button:IsA("TextButton")  then
				for _, frame in pairs(button:GetChildren()) do
					if frame:IsA("Frame") then
						local boughtLevels = frame:WaitForChild("boughtLevels")
						local BuyLvlPrice = frame:WaitForChild("BuyLvlPrice")
						local GiveMoneyReal = frame:WaitForChild("GiveMoneyReal")
						local GiveMoneyInHour = frame:WaitForChild("GiveMoneyInHour")
						Info1:SetAsync(plr.UserId, {boughtLevels.Value,BuyLvlPrice.Value,GiveMoneyReal.Value,GiveMoneyInHour.Value})

						print("Saved")
					end
				end
			end
		end
	end)
	if not success then
		warn("Error loading player data: " .. error)
	end
end

local function loadPlayerData(plr)
	local success, error = pcall(function()
		local PlayerGui = plr.PlayerGui
		for _, button in pairs(PlayerGui:WaitForChild("Game").Profit.Markets:GetChildren()) do
			if button:IsA("TextButton")  then
				for _, frame in pairs(button:GetChildren()) do
					if frame:IsA("Frame") then
						local currencyData = Info1:GetAsync(plr.UserId)
						
						local boughtLevels = frame:WaitForChild("boughtLevels").Value
						local BuyLvlPrice = frame:WaitForChild("BuyLvlPrice").Value
						local GiveMoneyReal = frame:WaitForChild("GiveMoneyReal").Value
						local GiveMoneyInHour = frame:WaitForChild("GiveMoneyInHour").Value
						
						if currencyData then
							local boughtLevels = currencyData[1]
							local BuyLvlPrice = currencyData[2]
							local GiveMoneyReal = currencyData[3]
							local GiveMoneyInHour = currencyData[4]
							print("Loaded")
						else
							print("Data not found for player")
						end
					end
				end
			end
		end
	end)
	if not success then
		warn("Error loading player data: " .. error)
	end
end

Players.PlayerAdded:Connect(function(plr)
	loadPlayerData(plr)
end)

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr,event)
	if event == "A" then
		savePlayerData(plr)
	else
		loadPlayerData(plr)
	end
end)

All printing, but info dont saving or loading. No errors in output

this script gives me medium headache. Please, rewrite this all.

1 - in my case, I can’t just instantiate all the values to the player. There are too many of them
2 - I can’t save data via PlayerRemoved because PlayerGui is being deleted
3 - What exactly causes you a headache? For cycles?

In any case. Do you have any idea what the mistake is?

store data in Leaderstats then access Player.Leaderstats because that won’t be deleted on PlayerRemoving (make sure to Instance.new(“Folder”,Player).Name=“Leaderstats” before saving data there)

im referencing this simple tutorial: In-experience leaderboards | Documentation - Roblox Creator Hub

“1 - in my case, I can’t just instantiate all the values to the player. There are too many of them”

then store it in local table like this (this is just example you should use getasync(plr.UserId) and updateasync(plr.UserId) with pcall)

local playerdata={}
local defaultData={
   coins=0
}


game:GetService("Players").PlayerAdded:Connect(function(plr)
        local data=datastore:GetAsync(plr.Name)
        if data==nil then
                playerdata[plr.Name]=defaultData
                return
        end
        playerdata[plr.Name]=data
end)

game:GetService("Players").PlayerRemoving:Connect(function(plr)
          if playerdata[plr.Name]==nil then
                  return
          end

         datastore:SetAsync(plr.Name,playerdata[plr.Name])
         playerdata[plr.Name]=nil
end)