roblox save to datastore code example

Example 1: how to save data roblox stuiod

local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PlayerGold")
 
-- Data store key and value
local playerUserID = 505306092 --could be Player.UserID
local playerGold = 250 --set somewhere else
 
-- Set data store key
local setSuccess, errorMessage = pcall(function()
	goldStore:SetAsync(playerUserID, playerGold)
end)
if not setSuccess then
	warn(errorMessage)
end
 
-- Read data store key
local getSuccess, currentGold = pcall(function()
	return goldStore:GetAsync(playerUserID)
end)
if getSuccess then
	print(currentGold)
end
--Credit: https://developer.roblox.com/en-us/articles/saving-data-introduction
--Hope this helps :)

Example 2: roblox studio data store

-- Get the DataStoreService
local DataStoreService = game:GetService("DataStoreService")

-- Get a DataStore
local myDataStore = DataStoreService:GetDataStore("myDataStore")

-- Get the Data
local Data = myDataStore:GetAsync(Player.UserId)

-- Set the Data
myDataStore:SetAsync(Player.UserId, Data)

Tags:

Misc Example