我的数据存储系统不工作,尽管我收到了成功保存的消息。Roblox

所以我做了一个保存Silver的数据存储系统。我使用了pcalls,每当玩家离开时,要么我没有收到消息,要么只是成功保存了,这很奇怪,我从来没有收到过任何错误。

但它并没有工作。我尝试在Roblox本身中进行,而不仅仅只是在Studio中。不起作用。这是在ServerScriptService中的服务器脚本。

请帮忙:D

local dataStore = game:GetService("DataStoreService"):GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(plr)

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr

    local silver = Instance.new("IntValue")
    silver.Name = "Silver"
    silver.Parent = leaderstats

    local playerUserId = "Player_"..plr.UserId

    local data
    local success, errormessage = pcall(function()
        data = dataStore:GetAsync(playerUserId)
    end)

    if success then
        silver.Value = data
    end


end)

game.Players.PlayerRemoving:Connect(function(plr)
    local playerUserId = "Player_"..plr.UserId

    local data = plr.leaderstats.Silver.Value

    local success, errormessage = pcall(function()
        dataStore:SetAsync(playerUserId, data)
    end)

    if success then
        print("数据成功保存。")
    else
        print("保存数据时出错。")
        warn(errormessage)
    end
end)```
点赞
用户14301077
用户14301077

Datastores 需要字符串作为键。你正在向 SetAsync 传递一个整数,你需要使用 tostring() 函数将其转换为字符串。

你的修改后的代码应该像这样。

local dataStore = game:GetService("DataStoreService"):GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(plr)

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr

    local silver = Instance.new("IntValue")
    silver.Name = "Silver"
    silver.Parent = leaderstats

    local playerUserId = "Player_"..plr.UserId

    local data
    local success, errormessage = pcall(function()
        data = dataStore:GetAsync(tostring(playerUserId))
    end)

    if success then
        silver.Value = data
    end


end)

game.Players.PlayerRemoving:Connect(function(plr)
    local playerUserId = "Player_"..plr.UserId

    local data = plr.leaderstats.Silver.Value

    local success, errormessage = pcall(function()
        dataStore:SetAsync(tostring(playerUserId), data)
    end)

    if success then
        print("数据已经成功保存。")
    else
        print("保存数据时出错。")
        warn(errormessage)
    end
end)
2021-01-26 09:33:06