从 roblox 中的 dataStore 获取数据的问题

问题描述

一个关于 Roblox Studio 的问题,或者更确切地说,关于 dataStore。如果在输出数据时通过 pointsstore:SetAsync ("Mars",19) 将值直接保存在脚本中:GetCurrentPage() - 此值是输出,但如果您通过函数执行此操作,该值将被保存,但不会data:GetCurrentPage() 时不出现。如何保存用户数据?

直接在脚本中保存值:

PlayerPoints:SetAsync("Mars",19)

local success,err = pcall(function()
    local Data = PlayerPoints:GetSortedAsync(false,5)
    local WinsPage = Data:GetCurrentPage()

    print(WinsPage)
end)

直接在函数中保存值:

local function givePointsPlayer(player,points)
    local pointsOld = pointsstore:GetAsync(player.Name.."&"..tostring(player.UserId).."&"..tostring(os.date("*t").month))
    if (pointsOld == nil) then
        pointsOld = 0
    end
    print(pointsOld)
    local success,err = pcall(function()
        pointsstore:SetAsync(
            player.Name.."&"..tostring(player.UserId).."&"..tostring(os.date("*t").month),pointsOld + points              
        )
    end)
end

EventEditPointsPlayer.OnServerEvent:Connect( function(player,points)
    givePointsPlayer(player,points)
end)

答案: answer

如何保存用户数据以便通过 :GetCurrentPage() 输出??

解决方法

如果要从页面中获取数据,则需要编写一些代码来遍历每个条目和页面。以下是 DevHub 教程中的示例:

-- Sort data into pages of three entries (descending order)
local pages = characterAgeStore:GetSortedAsync(false,3)

while true do
    -- Get the current (first) page
    local data = pages:GetCurrentPage()
    -- Iterate through all key-value pairs on page
    for _,entry in pairs(data) do
        print(entry.key .. ":" .. tostring(entry.value))
    end
    -- Check if last page has been reached
    if pages.IsFinished then
        break
    else
        print("----------------")
        -- Advance to next page
        pages:AdvanceToNextPageAsync()
    end
end

有关更多信息,请参阅 Roblox DevHub 的本教程:https://developer.roblox.com/en-us/articles/Data-store