How to save a value to the server?

Hello! So I ran into a problem I am using Messaging Service to send values across to all servers. So pretty much you click the button and it adds the value to all the servers. I made it were if one server is up it sends a message with the value to the second server and the value updates. But if everyone leaves and no servers are up then when somebody does join the value is 0 because it doesn’t save to the server.

So pretty much I want to save the value to the server. If you need any further information or don’t understand something tell me! :slight_smile:

Thank you!

1 Like

You can use datastore in a while loop so that whenever the datastore is update, all the servers can detect it and reload.

I meant like when all the players leave and join the game how would I make a data store for the server?

This article may help you with what you want to achieve. Saving Data to a Server rather than a player?

Nono lol. I want to save the value to the data store but the problem is the value has to be saved across all servers so pretty much in the server lol.

1 Like

But this wont work if all players leave the game

Not sure about this sorry I dont know if it is possible if ALL the players leave the game because then the server would be shutdown.

Yeah I am starting to lean towards that. Idk if there is a way to have a data store for the server instead of the player

Yeah if all players leave I think its impossible because it shuts down.

You could in theory use BindToClose to listen to the server being shut down.

Yes but even if he stored data to the server when the server was about to shutdown there would be no point because the server would be destroyed and the data wouldnt be able to be accessed.

You would have the value stored which you could then access when a server starts up.

yeah but how would you store the value inside the server instead of the player?

Same way you make playerdata but with a key that is not connected with the player. You could use the key “Server” which would look something like this:

local ds = game:GetService("DataStoreService"):GetDataStore("GlobalValue")
local SERVER_KEY_NAME = "Server"

local success, data = pcall(function()
    return ds:GetAsync(SERVER_KEY_NAME)
end)

if success then
    print(data) -->> the server data
end

-->> some saving logic down here
6 Likes