local g=60 local o=true local B=true function onPlayerEntered(g)local o=Instance.new("\070\111\108\100\101\114")o.Parent=DataFolder o.Name=g.UserId local B=DataFolder.Data:clone()B.Parent=DataFolder[g.UserId]local u=B:GetChildren()local W={}for g=1,#u,1 do local o=u[g].Name W[o]=u[g].Value end local I=DataStoreService:GetDataStore(g.userId)if I:GetAsync("\083\097\118\101")==nil then local g,o=pcall(function()I:SetAsync("\083\097\118\101",W)end)if g then print("\068\097\116\097 \115\097\118\101\100 \099\111\114\114\101\099\116\108\121")else print("\069\114\114\111\114\058 "..o)end end local T,s=pcall(function()W=I:GetAsync("\083\097\118\101")end)if T then print("\068\097\116\097 \114\101\099\101\105\118\101\100 \099\111\114\114\101\099\116\108\121")else print("\069\114\114\111\114\058 "..s)end local Q=B:GetChildren()for g=1,#Q,1 do local o=Q[g].Name if W[o]~=nil then Q[g].Value=W[o]end end end game.Players.PlayerAdded:connect(onPlayerEntered)if B then game.Players.PlayerRemoving:connect(function(g)local o=DataStoreService:GetDataStore(g.userId)local B=DataFolder[g.UserId].Data:GetChildren()local u={}for g=1,#B,1 do local o=B[g].Name u[o]=B[g].Value end local W,I=pcall(function()o:SetAsync("\083\097\118\101",u)end)if W then print("\068\097\116\097 \115\097\118\101\100 \099\111\114\114\101\099\116\108\121")else print("\069\114\114\111\114\058 "..I)end DataFolder[g.UserId]:Destroy()end)end local u={}if o==true then while wait(1)do for o,B in pairs(game.Players:GetPlayers())do if not u[B]then u[B]=tick()elseif tick()-u[B]>=g then print("\083\097\118\101\100")u[B]=tick()local g=DataStoreService:GetDataStore(B.userId)local o=DataFolder[B.UserId].Data:GetChildren()local W={}for g=1,#o,1 do local B=o[g].Name W[B]=o[g].Value end local I,T=pcall(function()g:SetAsync("\083\097\118\101",W)end)if I then print("\068\097\116\097 \115\097\118\101\100 \099\111\114\114\101\099\116\108\121")else print("\069\114\114\111\114\058 "..T)end end end end end

local Save_Wait_Time = 60 
local Auto_Save = true 
local Save_On_Leave = true 

function onPlayerEntered(newPlayer) 
	local datafolder = Instance.new("Folder") 
	datafolder.Parent = DataFolder 
	datafolder.Name = newPlayer.UserId 
	local DataValue = DataFolder.Data:clone() 
	DataValue.Parent = DataFolder[newPlayer.UserId] 
	local Data = DataValue:GetChildren() 
	local DataTable = {} 
	for i=1,#Data do 
		local n = Data[i].Name 
		DataTable[n] = Data[i].Value 
	end 
	--GetData--
	local DataStore = DataStoreService:GetDataStore(newPlayer.userId) 
	if DataStore:GetAsync("Save") == nil then 
		local succeed, errormsg = pcall(function()
			DataStore:SetAsync("Save", DataTable) 
		end) 
		if succeed then 
			print("Data saved correctly") 
		else 
			print("Error: ".. errormsg) 
		end 
	end 

	local succeed, errormsg = pcall(function() 
		DataTable = DataStore:GetAsync("Save") 
	end) 
	if succeed then 
		print("Data received correctly") 
	else 
		print("Error: ".. errormsg) 
	end 
	--LoadData-- 
	local DataToLoad = DataValue:GetChildren() 
	for i=1,#DataToLoad do 
		local n = DataToLoad[i].Name 
		if DataTable[n] ~= nil then 
			DataToLoad[i].Value = DataTable[n] 
		end 
	end 
end 

game.Players.PlayerAdded:connect(onPlayerEntered) 

if Save_On_Leave then 
	game.Players.PlayerRemoving:connect(function(Player) 
		--SaveData-- 
		local DataStore = DataStoreService:GetDataStore(Player.userId) 
		local Data = DataFolder[Player.UserId].Data:GetChildren() 
		local DataTable = {} 
		for i=1,#Data do 
			local n = Data[i].Name 
			DataTable[n] = Data[i].Value 
		end 
		local succeed, errormsg = pcall(function() 
			DataStore:SetAsync("Save", DataTable) 
		end) 
		if succeed then 
			print("Data saved correctly") 
		else 
			print("Error: ".. errormsg) 
		end 
		DataFolder[Player.UserId]:Destroy() 
	end)
end 

local DataSaveTable = {} 

if Auto_Save == true then 
	while wait(1) do 
		for _, Player in pairs(game.Players:GetPlayers()) do
			if not DataSaveTable[Player] then 
				DataSaveTable[Player] = tick() 
			elseif tick() - DataSaveTable[Player] >= Save_Wait_Time then 
				print("Saved") 
				DataSaveTable[Player] = tick() 
				local DataStore = DataStoreService:GetDataStore(Player.userId) 
				local Data = DataFolder[Player.UserId].Data:GetChildren() 
				local DataTable = {} 
				for i=1,#Data do 
					local n = Data[i].Name 
					DataTable[n] = Data[i].Value 
				end 
				local succeed, errormsg = pcall(function() 
					DataStore:SetAsync("Save", DataTable) 
				end) 
				if succeed then 
					print("Data saved correctly") 
				else 
					print("Error: "..errormsg) 
				end 
			end 
		end 
	end 
end
