Saving A Global Value / Updating it Real-Time

So, in my game there’s this button that counts how many times its been clicked.
Problem is, I want it to save on any server so its global and also display the amount clicked every times its been clicked in any server.

how would I go about with this?

You can use DatastoreService:GetDataStore() to save/load the value on servers.

Here’s a sample code:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Your Datastore name")
local myValue = game.ReplicatedStorage.MyValue

function load()
 local data 
 data = DataStore:GetAsync("Your Key")
 if data then
      print(data)
      myValue.Value = data
 else
      data = 0
   end
update()
end

function update()
  while wait(10) do
      local data = DataStore:GetAsync("KEY")
      myValue.Value = data
   end
end

wait(5)
load()

Please note that this is just a sample code and it’s very basic.
please read DatastoreService:GetDataStore() documentation for more info.

And also, if you want to add data, simply do


function Increment()
 
      local data = DataStore:IncrementAsync(1)
      
   
end

I also highly recommend using pcall

1 Like

Adding to @FelixProfit I recommend you to save/load the data every 10+ seconds to prevent data store from crashing.

Where should I add a pcall?
sorry for the very late response

Usually GetAsync(), SetAsync() and others