如果您使用
pointsStore:SetAsync("Mars", 19)
直接在脚本中保存值,则在输出 data: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)
答案:
我需要如何保存用户数据以便通过
: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