【问题标题】:Roblox data stores not updating dataRoblox 数据存储不更新数据
【发布时间】:2019-06-29 17:58:06
【问题描述】:

过去几天我一直卡在数据没有保存的问题上,我的代码没有出现任何错误,据我所知它应该可以工作。代码如下:

local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerStats001")


-- Function to make the leaderstats
function onPlayerJoin(player)

    local Leaderstats = Instance.new("Folder", player)
    Leaderstats.Name = ("leaderstats")

    local leadermoney = Instance.new("IntValue", Leaderstats)
    leadermoney.Name = ("Money")


    local leaderrescues = Instance.new("IntValue", Leaderstats)
    leaderrescues.Name = ("Rescues")


    local key = "player-" .. player.userId

    local savePoints = {}
    savePoints = DataStore:GetAsync(key)

    if savePoints then
        print("Old leaderstats")
        -- Has been to game before, save format = {money, rescues}
        leadermoney.Value = savePoints[1]
        leaderrescues.Value = savePoints[2]
    else
        print("New leaderstats")
        leadermoney.Value = 25000
        leaderrescues.Value = 0
        local valuesToSave = {leadermoney.Value, leaderrescues.Value}
        DataStore:SetAsync(key, valuesToSave)
    end

end

-- Saves player data
function save(player)
    print("Started save")
    local key = "player-" .. player.userId
    local valuesToSave = {player.leaderstats:FindFirstChild("Money").Value , player.leaderstats:FindFirstChild("Rescues").Value}
    DataStore:SetAsync(key, valuesToSave)
    print("Finished save", valuesToSave[1], valuesToSave[2])
end

function playerLeaves(player)
    save()
end

-- Runs the save function if a client requests a save
game.Lighting.RemoteEvents.Save.OnServerEvent:Connect(save)

-- Runs the playerLeaves function if a player leaves
game.Players.PlayerRemoving:Connect(save)

-- Runs the onPlayerJoin function when a player joins
game.Players.PlayerAdded:Connect(onPlayerJoin)

当我运行它时(我在启动器 GUI 中有一个本地脚本可以工作):

function autoSaveRequester()
    print("Started auto save function.")
    while wait(15) do
        print("Finnished wait")
        game.Lighting.RemoteEvents.Save:FireServer()
    end
end

spawn(autoSaveRequester)

它输出的值与我更改它之前的值相同,而不是新值,感谢任何帮助。

【问题讨论】:

  • 您看到的旧值是“新领导者统计”的默认情况吗?可能是 DataStores 不喜欢你给它一个信息数组吗?您是否尝试将数据保存为字符串?使用 HttpService:JSONEncode() 将数据表序列化为 json 字符串非常容易,然后当您想使用 HttpService:JSONDecode() 将其加载回表时。

标签: lua roblox


【解决方案1】:

由于这一行,您的代码无法正常工作;

game.Players.PlayerRemoving:Connect(save)

您直接跳转到保存函数,而不是 PlayerRemoving,也没有指定播放器。

将该行更新为:

game.Players.PlayerRemoving:Connect(playerLeaves)

如果这有帮助,请务必点赞!如果这回答了您的问题,请不要忘记按勾!

如果您还有其他需要,请在下方评论。

罗斯。

【讨论】:

    猜你喜欢
    • 2019-02-28
    • 2021-04-02
    • 1970-01-01
    • 2020-05-01
    • 2015-12-17
    • 1970-01-01
    • 2023-01-15
    • 2018-09-18
    • 2014-06-24
    相关资源
    最近更新 更多