Roblox studio datastore code example

Example 1: 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)

Example 2: Roblox DataStore

local DataStoreService = game:GetService("DataStoreService")
DataStore = DataStoreService:GetDataStore('DataStore')
local RunService = game:GetService("RunService")
--[[ This Imports The Data ]]--
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Value = DataStore:GetAsync(player.UserId) or 1
	stage.Parent = leaderstats
end)
--[[ This Saves The Data ]]--
game.Players.PlayerRemoving:Connect(function(player)
	local stage = player.leaderstats:WaitForChild("Stage")
	warn("Stage Value Changed")
	DataStore:SetAsync(player.UserId, stage.Value)
	print("Saved ".. stage.Value .. " Stage For " .. player.Name)
end)
--[[ This Avoids Data Loss ]]--
game:BindToClose(function()
	if RunService:IsStudio() then
		wait(7)
	end
end)

Tags:

Misc Example