Roblox Studio - How To Handle Data #2 [DataStoreService]

Опубликовано: 29 Апрель 2026
на канале: Kinfolk Studio
234
6

This video will show you how to use DataStoreService to create the SaveData and LoadData function in a module script, we will also cover briefly about pcall, coroutine, PlayerRemoving and BindToClose functions.

How To Handle Data #1 can be found here:    • Roblox Studio - How To Handle Data #1 [In-...  

DataStoreModule module script:
local module = {}

local DataStoreService = game:GetService("DataStoreService")
local PlayerDS = DataStoreService:GetDataStore("PlayerDS")
local DataStoreKey = "SecretCode_"
local DataLoaded = {}

function module.LoadData(Player)
DataLoaded[Player.UserId] = false
local Data
local Success, ErrMsg
repeat
Success, ErrMsg = pcall(function()
Data = PlayerDS:GetAsync(DataStoreKey..Player.UserId)
end)
if not Success then
print("Failed to load player's data.")
end
until Success or game.Players:FindFirstChild(Player)

if Success then
Player.SavedData.Level.Value = Data["Level"] or 0
Player.SavedData.Exp.Value = Data["Exp"] or 0
Player.SavedData.Gold.Value = Data["Gold"] or 0
DataLoaded[Player.UserId] = true
end

end

function module.SaveData(Player)
if DataLoaded[Player.UserId] == false then
print("Data not loaded")
return false
end

local Success, ErrMsg
repeat
Success, ErrMsg = pcall(function()
PlayerDS:UpdateAsync(DataStoreKey..Player.UserId, function(oldValue)
local newValue = {}
newValue["Level"] = Player.SavedData.Level.Value
newValue["Exp"] = Player.SavedData.Exp.Value
newValue["Gold"] = Player.SavedData.Gold.Value
return newValue
end)
end)
if not Success then
print("Failed to save data.")
task.wait(6)
else
print("Data saved.")
end
until Success
end

return module

CreatePlayerDate script: (modified on top of last video)
local Players = game:GetService("Players")

local ServerScriptService = game:GetService("ServerScriptService")
local DataStoreModule = require(ServerScriptService:WaitForChild("DataStoreModule"))

local function NewGameSetup(Player)

local InGameData = Instance.new("Folder", Player)
InGameData.Name = "InGameData"

local SavedData = Instance.new("Folder", Player)
SavedData.Name = "SavedData"

local leaderstats = Instance.new("Folder", Player)
leaderstats.Name = "leaderstats"

local Level = Instance.new("IntValue", SavedData)
Level.Name = "Level"

local Exp = Instance.new("IntValue", SavedData)
Exp.Name = "Exp"

local Gold = Instance.new("IntValue", SavedData)
Gold.Name = "Gold"

local SessionPlayedTime = Instance.new("IntValue", InGameData)
SessionPlayedTime.Name = "SessionPlayedTime"

local LevelLS = Instance.new("IntValue", leaderstats)
LevelLS.Name = "Level"

local GoldLS = Instance.new("IntValue", leaderstats)
GoldLS.Name = "Gold"
end

Players.PlayerAdded:Connect(function(Player)
NewGameSetup(Player)

Player.SavedData.Level:GetPropertyChangedSignal("Value"):Connect(function()
Player.leaderstats.Level.Value = Player.SavedData.Level.Value
end)

Player.SavedData.Gold:GetPropertyChangedSignal("Value"):Connect(function()
Player.leaderstats.Gold.Value = Player.SavedData.Gold.Value
end)

DataStoreModule.LoadData(Player)

end)

Players.PlayerRemoving:Connect(function(Player)
DataStoreModule.SaveData(Player)
end)

game:BindToClose(function()
for _, plr in ipairs(Players:GetPlayers()) do
coroutine.wrap(function()
DataStoreModule.SaveData(plr)
end)
end
end)

-- AutoSave
while true do
task.wait(300) -- 5 mins
for _, plr in ipairs(Players:GetPlayers()) do
coroutine.wrap(function()
DataStoreModule.SaveData(plr)
end)
end
end