Need some help on my sync system

Tried making an auto updating system that sync the data across every servers but it doesn’t seem to work. Somehow the table gets resorted or something and the server thinks the data is different so it updates the data again :confused:

no errors

local Sync = {};

print("Syning")

local DataStoreService = game:GetService("DataStoreService")
local DataStore = game:GetService("DataStoreService"):GetDataStore("Sync");
	
Sync.Data = {};



for _,Module in pairs(script:GetChildren()) do
	local Data = require(Module);
	Sync.Data[Module.Name] = Data;
end

warn(game:GetService("HttpService"):JSONEncode(Sync.Data));
warn(game:GetService("HttpService"):JSONEncode(DataStore:GetAsync("Sync2")));
local function CanRead()
	
	local budget = DataStoreService:GetRequestBudgetForRequestType(Enum.DataStoreRequestType.GetAsync);
	if budget > 0 then
		return true; else return false; 	
	end
	
end



local function LoadSync()																			
	if DataStore:GetAsync("Sync2") == nil then
		warn("First time loading sync, setting sync data")
		
		DataStore:SetAsync("Sync2", Sync.Data); 	
	end
	
	local NewData = game:GetService("HttpService"):JSONEncode((Sync.Data));
	local OldData = game:GetService("HttpService"):JSONEncode(DataStore:GetAsync("Sync2"));

	if NewData ~= OldData then
		warn("1: " .. OldData)
		warn("2: " .. NewData)
		DataStore:SetAsync("Sync2", Sync.Data); 														
		print("Sync Overrided");
	end
	
	spawn(function()
		while true do
			wait(10)
			if CanRead() then
				
			    local OldData = game:GetService("HttpService"):JSONEncode((Sync.Data));
				local NewData = game:GetService("HttpService"):JSONEncode(DataStore:GetAsync("Sync2"))
			
				if NewData ~= OldData then
					Sync.Data = DataStore:GetAsync("Sync2");
					
					warn("Updating Game...");
					game.ReplicatedStorage.Updating:FireAllClients();
					
				end
				
				
			end
			
			
			
		end
	end)

end

print("Syning...");

local Success,Error = pcall(LoadSync);

if Success then
	print("Synced");
else
	warn("Error: " .. Error);
	if CanRead() then
		local OldData = game:GetService("HttpService"):JSONEncode((Sync.Data));
		local NewData = game:GetService("HttpService"):JSONEncode(DataStore:GetAsync("Sync"));
			
		if NewData ~= OldData then
			Sync.Data = (NewData);
					
		end
				
				
	end																							
	
end

return Sync;

You have a bunch of prints and warns in your code but somehow you’re not able to tell us whether your code is receiving an error or not and can’t pinpoint where exactly the code is going wrong? I find that impossible. Please test your code again and try to point out where you’re encountering a source of error.

An exact issue should also be provided to make it easier to understand what your problem is and what to look into when attempting to provide solutions. It’s unclear what your current error is as you yourself don’t seem certain about what the unintended behaviour here is.

1 Like

There’s no errors, everything works except the table data gets resorted everytime and I’m unsure what’s the problem, still looking for the actual problem myself