Roblox:数据正在保存,但尝试加载时失败?

Roblox: Data is Saving, But Fails when trying to load?

所以我一直在尝试将某个“文本”从 TextLabel 保存到 DataStore,数据保存成功,但是在尝试加载时它给我的只是给我一个“失败”,任何帮助?

这是一个快速视频:https://www.youtube.com/watch?v=W-J6U8zmATk&feature=youtu.be

脚本:

local DataStoreService = game:GetService("DataStoreService")
local IDStorage = DataStoreService:GetDataStore("IDStorage3")

elseif Player.Team.Name == "Intelligence Agency" then
            if Player:IsInGroup(7503826) or Player:GetRankInGroup(7465879) >= 251 then
                Rank.User.Text = "[REDACTED]"
                Rank.User.Back.Text = "[REDACTED]"
                Rank.Rank.TextColor3 = Color3.new(0.827451, 0, 0)
            
                     
                game.ReplicatedStorage.NewID.OnServerEvent:Connect(function(player, playerToID, AssignedID)
                    if player:IsInGroup(7465879) then
                        local success, err = pcall(function()
                            IDStorage:SetAsync(playerToID, AssignedID)
                        end)
                            
                        if success then
                            print("Data Assigned") -- Data Works and Saves
                        else
                            warn("Failed to Save")
                        end
                    end
                end)
                
                
                local ID = IDStorage:GetAsync(Player)

                if ID then
                    print(ID) 
                else
                    warn("Failed") -- Always Returns me this.
                    Rank.Rank.Text = "0"
                end
                
                    Rank.User.Text = "[REDACTED]"
                    Rank.User.Back.Text = "[REDACTED]"
                end

DataStore:GetAsync(key) 函数需要一个字符串作为键。看起来你传递的是 Player 对象而不是玩家的用户名,你说这是你存储数据的密钥。

换行试试这个

local ID = IDStorage:GetAsync(Player)

为此:

local success, result = pcall(function()
    local key = Player.Name
    return IDStorage:GetAsync(key)
end
if success then
    print("Got Id : ", result)
    local ID = result
    if ID then
        -- do stuff with the result
    else
        -- looks like a new player with no saved data
    end
else
    warn("failed to get id with error :", result)
    -- do something to handle the error, like retry
end

保存数据时要注意的一点是用户名可能会改变。如果我要更改我的名字,下次我加入这个游戏时,我所有的进步都会消失,因为名字与保存的密钥不匹配。这就是为什么 player's userId 往往是推荐键。