PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

GlobalDataStore

Show deprecated

GlobalDataStore

Show deprecated

A GlobalDataStore exposes functions for saving and loading data for the DataStoreService.

See the Articles/Data store|Data Stores article for an in-depth guide on data structure, management, error handling, etc.

Properties

Inherited from Instance: Show Hide

bool

Archivable

Determines if an Instance can be cloned using /Instance/Clone or saved to file.

string

ClassName

[ReadOnly] [NotReplicated]

A read-only string representing the class this Instance belongs to

int

DataCost

[ReadOnly] [NotReplicated] [Deprecated]

The cost of saving the instance using data persistence.

string

Name

A non-unique identifier of the Instance

Instance

Parent

Determines the hierarchical parent of the Instance

bool

RobloxLocked

If true, the Instance and its descendants cannot be indexed or edited by a Script or LocalScript and will throw an error if it is attempted

bool

archivable

[Hidden] [NotReplicated] [Deprecated]
string

className

[ReadOnly] [NotReplicated] [Deprecated]

Functions

Variant

GetAsync ( string key )

[Yields]

Returns the value of the entry in the data store with the given key

Variant

IncrementAsync ( string key , int delta )

[Yields]

Increments the value of a particular key and returns the incremented value

RBXScriptConnection

OnUpdate ( string key , Function callback )

[Deprecated]

Sets callback as a function to be executed any time the value associated with key is changed

Variant

RemoveAsync ( string key )

[Yields]

Removes the given key from the data store and returns the value associated with that key

void

SetAsync ( string key , Variant value )

[Yields]

Sets the value of the key. This overwrites any existing data stored in the key.

Tuple

UpdateAsync ( string key , Function transformFunction )

[Yields]

Retrieves the value of a key from a data store and updates it with a new value

Inherited from Instance: Show Hide

void

ClearAllChildren ( )

This function destroys all of an Instance's children.

Instance

Clone ( )

Create a deep copy of a Roblox instance and descendants where Archivable = true.

void

Destroy ( )

Sets the Instance/Parent property to nil, locks the Instance/Parent property, disconnects all connections and calls Destroy on all children.

Instance

FindFirstAncestor ( string name )

Returns the first ancestor of the Instance whose Instance/Name is equal to the given name.

Instance

FindFirstAncestorOfClass ( string className )

Returns the first ancestor of the Instance whose Instance/ClassName is equal to the given className.

Instance

FindFirstAncestorWhichIsA ( string className )

Returns the first ancestor of the Instance for whom Instance/IsA returns true for the given className.

Instance

FindFirstChild ( string name , bool recursive )

Returns the first child of the Instance found with the given name.

Instance

FindFirstChildOfClass ( string className )

Returns the first child of the Instance whose Instance/ClassName|ClassName is equal to the given className.

Instance

FindFirstChildWhichIsA ( string className , bool recursive )

Returns the first child of the Instance for whom Instance/IsA returns true for the given className.

Variant

GetAttribute ( string attribute )

RBXScriptSignal

GetAttributeChangedSignal ( string attribute )

Dictionary

GetAttributes ( )

Objects

GetChildren ( )

Returns an array containing all of the Instance's children.

string

GetDebugId ( int scopeLength )

[NotBrowsable]

Returns a coded string of the Instances DebugId used internally by Roblox.

Array

GetDescendants ( )

[CustomLuaState]

Returns an array containing all of the descendants of the instance

string

GetFullName ( )

Returns a string describing the Instance's ancestry.

RBXScriptSignal

GetPropertyChangedSignal ( string property )

Get an event that fires when a given property of an object changes.

bool

IsA ( string className )

[CustomLuaState]

Returns true if an Instance's class matches or inherits from a given class

bool

IsAncestorOf ( Instance descendant )

Returns true if an Instance is an ancestor of the given descendant.

bool

IsDescendantOf ( Instance ancestor )

Returns true if an Instance is a descendant of the given ancestor.

void

Remove ( )

[Deprecated]

Sets the object’s Parent to nil, and does the same for all its descendants.

void

SetAttribute ( string attribute , Variant value )

Instance

WaitForChild ( string childName , double timeOut )

[CustomLuaState] [CanYield]

Returns the child of the Instance with the given name. If the child does not exist, it will yield the current thread until it does.

Objects

children ( )

[Deprecated]

Returns an array of the object’s children.

Instance

clone ( )

[Deprecated]
void

destroy ( )

[Deprecated]
Instance

findFirstChild ( string name , bool recursive )

[Deprecated]
Objects

getChildren ( )

[Deprecated]
bool

isA ( string className )

[Deprecated] [CustomLuaState]
bool

isDescendantOf ( Instance ancestor )

[Deprecated]
void

remove ( )

[Deprecated]

Events

Inherited from Instance: Show Hide

RBXScriptSignal

AncestryChanged ( Instance child , Instance parent )

Fires when the Instance/Parent property of the object or one of its ancestors is changed.

RBXScriptSignal

AttributeChanged ( string attribute )

RBXScriptSignal

Changed ( string property )

Fired immediately after a property of an object changes.

RBXScriptSignal

ChildAdded ( Instance child )

Fires when an object is parented to this Instance.

RBXScriptSignal

ChildRemoved ( Instance child )

Fires when a child is removed from this Instance.

RBXScriptSignal

DescendantAdded ( Instance descendant )

Fires when a descendant is added to the Instance

RBXScriptSignal

DescendantRemoving ( Instance descendant )

Fires immediately before a descendant of the Instance is removed.

RBXScriptSignal

childAdded ( Instance child )

[Deprecated]

Code Samples


Individual Player Data Stores

This code sample features a simple way to work with per-player data stores called “Stats” and “Resources.”

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local function onPlayerAdded(player)
	local scope = "Player_" .. player.UserId
	local playerStats = DataStoreService:GetDataStore("Stats", scope)
	local playerResources = DataStoreService:GetDataStore("Resources", scope)
	-- GetAsync()/UpdateAsync() can now be called on the 'Stats' and 'Resources' data stores
end

Players.PlayerAdded:Connect(onPlayerAdded)

Data Store Game Config

This code sample uses data stores to read/write/listen to a global configuration value. It uses a defensive coding style so potential data store errors will not cause issues.

local DATA_STORE_NAME = "Config"
local DATA_STORE_SCOPE = "MyGameName"
local KEY_NEW_FEATURE = "NewFeatureEnabled"

local DataStoreService = game:GetService("DataStoreService")

local dsConfig = DataStoreService:GetDataStore(DATA_STORE_NAME, DATA_STORE_SCOPE)

local function checkFeature()
	local isFeatureEnabled
	local success, err = pcall(function()
		isFeatureEnabled = dsConfig:GetAsync(KEY_NEW_FEATURE)
	end)
	if success then
		print(KEY_NEW_FEATURE .. ": " .. tostring(isFeatureEnabled))
		if isFeatureEnabled == true then
			print("Feature is enabled!")
		elseif isFeatureEnabled == false then
			print("Feature is disabled!")
		elseif isFeatureEnabled == nil then
			print("Feature is not set!")
		else
			-- Some other value was found in this key
		end
	else
		print("Failed to load feature! Error: " .. tostring(err))
	end
	return success
end

local function onFeatureChanged(isFeatureEnabled)
	print("Feature toggled: " .. tostring(isFeatureEnabled))
end

-- Listen for changes
dsConfig:OnUpdate(KEY_NEW_FEATURE, onFeatureChanged)

local function setFeatureEnabled(isFeatureEnabled)
	local success, err = pcall(function()
		dsConfig:SetAsync(KEY_NEW_FEATURE, isFeatureEnabled)
	end) 
	return success
end

setFeatureEnabled(true)
checkFeature()