How do I sync/save a value across all servers in a game?

Hello!
I’m trying to make a game where every time a player clicks a part, a number goes up by 1 (cooldown of 15 seconds). This number is shared so if 2 players click the part it becomes 2. What I want to aim for is this number being synced across all servers within the game, so if one player clicks the button, and in a different server 3 people click the button, 4 will show up as the number.

I know that this can’t be achieved in real time, but I was hoping to get advice on how to make a system where something like every 5 minutes (or every minute if it does not reach the limits of messagingservice), all the servers calculate how much the number has increased by and then replicate it to all the servers what the final number is. Ideally right after this calculation all the servers would have the same number until players start clicking the part, and it will be out of sync for 5 minutes until there is another calculation that tallies up again how much increase there was in total over all servers. In the end, every player’s click is counted towards the final number.

A few worries I have:

  • I assume I would use messaging service to tally up the increases for each server, but what server does the tallying and calculation, and then returns the final synced amount to all the rest of the servers?
  • How do I datastore the value properly, so that if one server has the number 10 and another has 8, but the server with 10 saves the key as it shuts down and then the server with 8 overwrites that number?
  • Do I just save the value of the number in a datastore with a single key? Example, do I do:
    game:GetService(“DataStoreService”):GetDatastore(“Number”):GetAsync(“Number”)
    for getting this single key / number value, and use UpdateAsync/GetAsync for getting/updating the value in the datastore?

Thanks!

4 Likes

MessagingService would be your best bet. That’s as far as I’m aware the only way to update a value across servers.

Don’t think it matters which server does the calculation. I’d use the server the most players are in that are clicking the part. (So let’s say you have a server >> 2 people are clicking the part, Let’s call this server A. And you have another server with only 1 player clicking it, I would make A your “main” server)

Found this forum. You should be able to use that with this and figure it out :smiley:

4 Likes

Awesome, thanks for the information! I assume for my 2nd worry I would be able to use updateasync to prevent overwriting the number with a lower value?

Also for getting the datastore, do I simply just use one key? I’ve only ever used datastores using player userIds as the key and never a single value for the whole game

did you ever end up achiving this? if so mind sharing how?

1 Like