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

时间:2020-10-21 01:50:22

标签: roblox

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

这是一个快速视频: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

1 个答案:

答案 0 :(得分:1)

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往往是推荐键的原因。