admin管理员组文章数量:1406063
I've been having trouble with this, because in the entire script I getAsync and SetAsync Values the exact same order, yet while troubleshooting ,because I got data loss constantly, it apparently says The 1st value it tries to set for the player is nil, although printing the table shows that it's there, just in a different position than i had set it.
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)
I've been having trouble with this, because in the entire script I getAsync and SetAsync Values the exact same order, yet while troubleshooting ,because I got data loss constantly, it apparently says The 1st value it tries to set for the player is nil, although printing the table shows that it's there, just in a different position than i had set it.
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)
Share
Improve this question
edited Mar 6 at 20:20
Kylaaa
7,2452 gold badges18 silver badges31 bronze badges
asked Mar 6 at 16:56
GrandpaKartulGrandpaKartul
155 bronze badges
2
|
1 Answer
Reset to default 0As you mentioned in your comment, the error you are seeing is :
Attempt to index nil with
Money
And you've already narrowed it down that it isn't your data
table. That means that your leaderstats ls
is nil. So looking at where you defined it...
local ls = plr:FindFirstChild("leaderstats")
It looks like you've got a race-condition. You are trying to fetch the leaderstats, but if it doesn't exist then it will return nil
. So if another script is creating it, one simple fix might be to change that line to...
local ls = plr:WaitForChild("leaderstats", 10)
But a better fix might be to move the code that constructs your leaderstats folder and all of the NumberValues into the top of this function instead. That way, the code that constructs the leaderstats is the same code that populates it with data.
game.Players.PlayerAdded:Connect(function(plr)
-- create the leaderstats
local ls = Instance.new("Folder")
ls.Name = "leaderstats"
local moneyVal = Instance.new("NumberValue", ls)
moneyVal.Name = "Money"
local levelVal = Instance.new("NumberValue", ls)
levelVal.Name = "Level"
local xpVal = Instance.new("NumberValue", ls)
xpVal.Name = "XP"
ls.Parent = plr
-- fetch data from the data stores
local retryCount = 0
local success = false
local result
while not success and retryCount < MAXIMUM_RETRY_ATTEMPTS do
本文标签: robloxDatastore player39s data in different order than setasyncStack Overflow
版权声明:本文标题:roblox - Datastore player's data in different order than setasync - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744960848a2634645.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
warn(string.format("Failed to Import %s's data with error : %s", plr.Name, tostring(result or "")))
?. Also, try moving theprint("Success Import")
andprint(data)
lines above thels.Money
line. It may show that you are correctly fetching the data but just not reading the values properly. – Kylaaa Commented Mar 6 at 18:39