How to Save Data (1 Minute) - Roblox Studios

Опубликовано: 26 Октябрь 2023
на канале: Maze Sunshine
33,918
486

Don't click this!    / @mazesunshine3946  
Int Value or Number value Script

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Saver = DataStoreService:GetDataStore("SaveLeaderstats")

-- Function to save leaderstats data for a player
local function Save(player)
local SavedData = {}
local leaderstats = player:FindFirstChild("leaderstats")

if leaderstats then
for _, v in pairs(leaderstats:GetChildren()) do
if v:IsA("IntValue") or v:IsA("NumberValue") then
SavedData[v.Name] = v.Value
end
end

local success, errormessage = pcall(function()
Saver:SetAsync(player.UserId, SavedData)
end)

if not success then
error(errormessage)
end
end
}

Players.PlayerAdded:Connect(function(player)
local Data = nil
local success, errormessage = pcall(function()
Data = Saver:GetAsync(player.UserId)
end)

if success then
if Data then
for i, v in pairs(Data) do
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats and leaderstats:FindFirstChild(i) then
leaderstats:FindFirstChild(i).Value = v
end
end
end
end
})

Players.PlayerRemoving:Connect(function(player)
Save(player)
})

game:BindToClose(function()
for _, player in pairs(Players:GetPlayers()) do
Save(player)
end
})

String Value Script

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Saver = DataStoreService:GetDataStore("SaveLeaderstats")

Players.PlayerAdded:Connect(function(player)
local Data = nil
local success, errormessage = pcall(function()
Data = Saver:GetAsync(tostring(player.UserId))
end)

if success then
if Data then
for i, v in pairs(Data) do
player:WaitForChild("leaderstats"):WaitForChild(i).Value = v
end
end
else
error(errormessage)
end
end)

local function Save(player)
local SavedData = {}
for _, v in pairs(player.leaderstats:GetChildren()) do
SavedData[v.Name] = v.Value
end

local success, errormessage = pcall(function()
Saver:SetAsync(tostring(player.UserId), SavedData)
end)
if not success then
error(errormessage)
end
end

Players.PlayerRemoving:Connect(Save)

game:BindToClose(function()
for _, v in pairs(Players:GetPlayers()) do
Save(v)
end
end)


Смотрите видео How to Save Data (1 Minute) - Roblox Studios онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Maze Sunshine 26 Октябрь 2023, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 33,91 раз и оно понравилось 48 людям.