How to update a value across servers

Hello!
I am trying to make a system where you can rate a map (like/dislike). I want those values to be saved and updated across all servers. I already have the datastore script written but I am worried about these things:

  • I am using Updateasync, but I am worried if I used it correctly and it won’t prevent overwriting data.

  • I noticed that the map votes only update only when you rejoin a server even though I am updating the datastore every 15 seconds (for testing purposes and will likely take longer in the final product).

local DataStoreService = game:GetService("DataStoreService")
local VotingStore = DataStoreService:GetDataStore("Votes")
local ServerStorage = game:GetService("ServerStorage")
local VotingValues = ServerStorage.VotingValues

local function LoadVotes()
	local data
	local success, Error = pcall(function()
		data = VotingStore:GetAsync("VotesData")
	end)
	if success then
		if data then
			print("Votes Data Loaded!")
			VotingValues.Alder.Likes.Value = data.Alder.Likes
			VotingValues.Alder.Dislikes.Value = data.Alder.Dislikes
		end
	else
		print(Error)
	end
end

local function UpdateVotes(CurrentData, keyInfo)
	local Mydata = {
		Alder = {Likes = VotingValues.Alder.Likes.Value, Dislikes = VotingValues.Alder.Dislikes.Value}
	}
	
	local success, Error, keyInfo = pcall(function()
	VotingStore:UpdateAsync("VotesData", function(ov)
		for i,v in pairs(Mydata) do
			ov[i] = v
		end
		return ov
		end)
	end)
	if success then
		print("Votes Data Updated!")
	end
end


while true do
	LoadVotes()
	wait(10)
	UpdateVotes()
end

Thanks!

You will probably need to include MessagingService in some capacity to update other servers when somebody from one server uplikes.
https://developer.roblox.com/en-us/api-reference/class/MessagingService

With the UpdateAsync, you could possibly designate one server to be the “datastore updater” server using the MessagingService. Maybe the first server created? And when that server is shutdown, you could send a message through MessagingService to designate a new server? These are just ideas.

1 Like

Master Server // Cross Server MSS Sync - Resources / Community Resources - DevForum | Roblox

Use a master server, then when someone votes you can send the ‘task’ of updating only to the master server, in the task you can add a GUID so that once completed you can return a task back with the same GUID to let the sender server know it has completed successfully or unsuccessfully

Just use messagingservice that will be best solution