How to save a value in a server?

Everything started here: Help on making a global message system - #11 by Conejin_Alt, asking how do i fix the script but i didn’t got response

  1. What do you want to achieve? Keep it simple and clear!
    I’m making a global announcement script, and i want to it save when the server gets removed and added again

  2. What is the issue? Include screenshots / videos if possible!
    My script that i tried doesn’t works, i did something bad, but i can’t fix it

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I asked the guy who told me about datastores (and i saw threads) but i didn’t understand.

Script:

local DataStoreService = game:GetService("DataStoreService")
local msgstore = DataStoreService:GetDataStore("MessageStore") --Value that saves the message
   
local success, err = pcall(function()
	msgstore:GetAsync(game.Workspace.GlobalAnnouncement.Value)
end)

local key = game.Workspace.GlobalAnnouncement.Value   

if success then
	print("Success!")
else
	msgstore:SetAsync(key, "ohnub")
end

I hope you can help me. Thanks

1 Like

The Roblox Developer Hub has wonderful resources on topics you may have questions about.

For this situation, please read over the article on Data Stores: Data Stores | Documentation - Roblox Creator Hub

If you’re interested, the DataStore2 module is also a great alternative as it’s more secure: How to use DataStore2 - Data Store caching and data loss prevention

Do you have any errors? Perhaps you have API access disabled under your game?

I suggest to use MessageService to send messages cross-server.
See more at Cross-Server Messaging | Documentation - Roblox Creator Hub

I
The script error prints that key is empty

I did that, now I want to know is how to save the message

In short you are calling to get a key and then doing nothing with it.

local success, err = pcall(function()
    msgstore:GetAsync(game.Workspace.GlobalAnnouncement.Value)
end)

This part right here, you request the API to get the value from a datastore at a key, but you do nothing with it. Also you seem to be calling the key as the last value you have which is more than likely going to be blank.

local success, key = pcall(function()
    return msgstore:GetAsync(<the key in the datastore where the message is saved>)
end)

If these announcements are ephemeral (for example, they show up in livetime in the chat but don’t need to be called later) then the MessageService is a better option, otherwise take some time to read over the DevHub information on DataStores as linked by CodeJared.