local key = "Player_" .. player.UserIdlocal success, data = pcall(function()
return playerDataStore:GetAsync(key)
end)
if success then
print("Данные загружены")
else
warn("Ошибка загрузки:", data)
endlocal success, err = pcall(function()
playerDataStore:SetAsync(key, data)
end)
if success then
print("Данные сохранены")
else
warn("Ошибка сохранения:", err)
endif typeof(coins) == "number" then
print("Данные корректны")
else
warn("Coins имеют неправильный тип")
endlocal data = {
Coins = 100,
Level = 5,
XP = 250
}local success, result = pcall(function()
return playerDataStore:UpdateAsync(key, function(oldData)
oldData = oldData or {}
oldData.Coins = 500
return oldData
end)
end)while true do
task.wait(120)
for _, player in ipairs(Players:GetPlayers()) do
savePlayerData(player)
end
endCoins.Value += 1
savePlayerData(player)Coins.Value += 1if data == nil then
data = {
Coins = 0,
Level = 1,
XP = 0
}
endlocal Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerData")
local function savePlayerData(player)
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then
warn("leaderstats не найден у " .. player.Name)
return
end
local coins = leaderstats:FindFirstChild("Coins")
local level = leaderstats:FindFirstChild("Level")
if not coins or not level then
warn("Не найдены Coins или Level у " .. player.Name)
return
end
local data = {
Coins = coins.Value,
Level = level.Value
}
local key = "Player_" .. player.UserId
local success, err = pcall(function()
playerDataStore:SetAsync(key, data)
end)
if success then
print("Данные сохранены для " .. player.Name)
else
warn("Ошибка сохранения:", err)
end
end
Players.PlayerRemoving:Connect(function(player)
savePlayerData(player)
end)