我一直遇到这个问题,因为在整个脚本中我 getAsync 和 SetAsync 值的顺序完全相同,但在排除故障时,由于我不断丢失数据,它显然说它尝试为玩家设置的第一个值是 nil,虽然打印表格显示它在那里,只是与我设置的位置不同。
local DataStore = game:GetService("DataStoreService"):GetDataStore("Basic")
local template = {
Money = 0;
Level = 0;
XP = 0
}
local MAXIMUM_RETRY_ATTEMPTS = 5
local RETRY_COOLDOWN = 1
game.Players.PlayerAdded:Connect(function(plr)
local ls = plr:FindFirstChild("leaderstats")
local retryCount = 0
local success = false
local result
while not success and retryCount < MAXIMUM_RETRY_ATTEMPTS do
success, result = pcall(function()
local data = DataStore:GetAsync(tostring(plr.UserId))
if data == nil then
print("nothing?")
else
ls.Money.Value = data.Money
ls.Level.Value = data.Level
ls.XP.Value = data.XP
print("Success import")
print(data)
end
end)
if not success then
retryCount = retryCount + 1
warn(string.format("Failed to Import %s's data with error : %s", plr.Name, tostring(result or "")))
local data = DataStore:GetAsync(tostring(plr.UserId))
print(data)
task.wait(RETRY_COOLDOWN)
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local result
local success = false
local ls = plr:FindFirstChild("leaderstats")
while not success do
success, result = pcall(function()
DataStore:SetAsync(tostring(plr.UserId), {
Money = ls.Money.Value;
Level = ls.Level.Value;
XP = ls.XP.Value
})
print("Success export")
end)
if not success then
warn(string.format("Failed to Export %s's data with error : %s", plr.Name, tostring(result or "")))
end
end
end)