如果我迅速获得战利品然后离开,为啥这个 roblox 脚本不会保存?

Posted

技术标签:

【中文标题】如果我迅速获得战利品然后离开,为啥这个 roblox 脚本不会保存?【英文标题】:Why won't this roblox script save if I quickly gain loot and then leave?如果我迅速获得战利品然后离开,为什么这个 roblox 脚本不会保存? 【发布时间】:2019-06-23 06:03:26 【问题描述】:

我制作了这个脚本来加载和保存数据。如果玩家加入和退出非常快,我该如何保存?如果我快速离开它会丢弃数据吗?我尝试了几件事,但它们似乎都不起作用。如果有人很快加入,收到很多战利品,然后退出,一切都被破坏了,那就太糟糕了。有什么方法可以告诉服务器在关闭之前将其全部保存?

local Database = game:GetService("DataStoreService"):GetDataStore("MAIN")
local RStorage = game:GetService("ReplicatedStorage")
local PlayerIndex = 
local CanSaveData = true
local BFunctionF = Instance.new("Folder", RStorage)
function SaveData(Player)
    local SCS, Result = pcall(function()
        Database:SetAsync("USER"..Player.UserId, PlayerIndex[Player])
        while Database:GetAsync("USER"..Player.UserId) ~= PlayerIndex[Player] do
            Database:SetAsync("USER"..Player.UserId, PlayerIndex[Player])
            warn("Stupid")
        end
        CanSaveData = true
    end)
    if not SCS then
        warn(Result)
    end
end
BFunctionF.Name  = "DataManagerBindableFunctions"
game.Players.PlayerAdded:Connect(function(Player)
    local ChangeData = Instance.new("BindableFunction", BFunctionF)
    ChangeData.Name = Player.UserId

    local Stats = Instance.new("BoolValue", Player)
    Stats.Name = "leaderstats"  

    local LevelStat = Instance.new("IntValue", Stats)
    LevelStat.Name = "Level"

    local CrystalStat = Instance.new("IntValue", Stats)
    CrystalStat.Name = "Crystals"

    local MoneyStat = Instance.new("IntValue", Stats)
    MoneyStat.Name = "Money"

    local SCS, Data = pcall(function()
        local RawData = Database:GetAsync("USER"..Player.UserId) or 
        local NewData = 
            ['Level'] = RawData['Level'] or 0,
            ['LvlXP'] = RawData['LvlXP'] or 0,
            ['Money'] = RawData['Money'] or 0,
            ['Crystal'] = RawData['Crystal'] or 0,
            ['Bonker'] = RawData['Bonker'] or 0,
            ['Sword'] = RawData['Sword'] or 0,
            ['Hammer'] = RawData['Hammer'] or 0,
            ['Knife'] = RawData['Knife'] or 0,
            ['Brick'] = RawData['Brick'] or 0
        return NewData
    end)

    if SCS and Data then
        PlayerIndex[Player] = Data
    end

    MoneyStat.Value = Data.Money
    LevelStat.Value = Data.Level
    CrystalStat.Value = Data.Crystal

    ChangeData.OnInvoke = function(Type, Amount)
        print(Player, Type, Data[Type])
        if Data[Type] == nil then
            warn("Fake Playerdata Type")
        else
            local NV = Data[Type]+Amount
            if Type == "Crystal" then
                CrystalStat.Value = NV
            elseif Type == "Money" then
                MoneyStat.Value = NV
            elseif Type == "Level" then
                LevelStat.Value = NV
            end
            Data[Type] = NV
        end
        print(Data[Type])
        return Data[Type]   
    end
end)

game.Players.PlayerRemoving:Connect(function(Player)
    if CanSaveData then
        CanSaveData = false
        SaveData(Player)
    end
end)
game:BindToClose(function()
    for O, Player in pairs(game.Players:GetPlayers()) do
        if CanSaveData then
            CanSaveData = false
            SaveData(Player)
        end
    end
end)

我该如何解决这个问题?

【问题讨论】:

【参考方案1】:

我的建议是停止使用异步来保存玩家数据。例如,如果GetAsync() 失败,则可能会将玩家数据设置为 false 或 nil。如果将此值写入数据存储区,则播放器的数据将被完全覆盖。如果玩家在服务器之间跳得太快,GetAsync() 也可以检索到不正确的先前数据。为了防止这种情况,我们使用UpdateAsync()

【讨论】:

以上是关于如果我迅速获得战利品然后离开,为啥这个 roblox 脚本不会保存?的主要内容,如果未能解决你的问题,请参考以下文章

ASP中的server.Mappath方法可以获得()

C# WinForm 文本框离开焦点事件 问题???

失速为啥会使得机翼升力迅速下降?

当我迅速从该视图控制器移动时,为啥我的 Collectionview 单元格没有显示?

为啥这个例子不离开

倒卖战利品