Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What is DataStore and when do you use it?

I'm guessing that it's like a server sending data to the "studio", like in admin scripts where you can PBan or time ban people in-game and that person can't join any server again, / like application centers in Cafe groups that don't use Trello/anything else like that. However, I am unsure about that. Am I right? If I'm wrong then what is a service/way to send data to the "studio game" or whatever? I hope I don't get you confused.

2 answers

✓ Accepted answer

###Data stores are a storage feature for Roblox games that can be used to save data that will persist even after a game’s server has shut down. They are designed to store custom player data or to control global game configurations. Data stores are shared per game, so any place in a game, including places in different servers, can access and change the same data.

#Structure

###A data store is essentially a dictionary, like a Lua table. Each value in the data store can be indexed by a unique key. For example, if a game needed to save how much experience each player had, it could add to a data store using the player’s user id as the key and the player’s experience as the value. Any place that needed to know how much experience a given player has could access the data store and look up that player’s experience with that player’s id.

###~ROBLOX Dev

DataStoreService is a way to store data. There are multiple different types of DataStores, but I usually use GetAsync and SetAsync. With these you can save stats, and load them. For example,

Folder = game.ReplicatedStorage.Data:Clone()
DataStoreService = game:GetService('DataStoreService')
Money = DataStoreService:GetDataStore('Money')

local function onPlayerAdded(player)
	Folder.Parent = player
	local playerFolder = player.Data
	
    local id = player.UserId
	local savedMoney = Money:GetAsync(id)
	
	playerFolder.Money.Value = savedMoney
	
	while wait(60) do
		Money:SetAsync(id, playerFolder.Money.Value)
	end
end

local function onPlayerRemoving(player)
	local id = player.UserId
	local playerFolder = player.Data
	
	Money:SetAsync(id, playerFolder.Money.Value)
end

game.Players.PlayerAdded:connect(onPlayerAdded)

If you want more info, read through this article on the roblox wiki page.

http://robloxdev.com/articles/Data-store

8 comments

0

Do not give deprecated code.

0

And GetAsync / SetAsync are not DataStores, they are methods of the GlobalDataStore / OrderedDataStore classes.

0

What is deprecated code?

0

line 26. connect is deprecated, you should be using Connect.

0

Oh come on, it still works fine.

0

At least I'm using the DataStoreService correctly.

0

Just because it works doesn't mean it should be used.

0

Alright, whatever you say.

This discussion is preserved as it was. New answers, comments, and votes are closed.