【问题标题】:How do I make a saving death counter leaderstats我如何制作拯救死亡计数器leaderstats
【发布时间】:2020-08-08 03:29:57
【问题描述】:

所以我正在尝试制作游戏,但无法保留统计数据。我已经有一个有效的死亡计数器,但不知道如何保存它,每次我重新加入时它都会重置。这是我尝试过的两个代码:

1)

local pointsDataStore = game:GetService("DataStoreService"):GetDataStore("Deaths")

game.Players.PlayerAdded:Connect(function(player)
    local playerKey = "Player_" .. player.UserId

    local success, err = pcall(function()
        pointsDataStore:UpdateAsync(playerKey, function(oldValue)
            local newValue = oldValue or 0
            newValue = newValue + 0
            return newValue
        end)
    end)
end)

2)

local DS = game:GetService("DataStoreService"):GetDataStore("Points")

game.Player.PlayerAdded:connect(plr)
    local leaderstats = Instance.new("Model",plr)
    leaderstats.Name = "leaderstats"

    local currency = Instance.new("IntValue", leaderstats)
    currency.Name = "Deaths"
    currency.Value = 1

   while wait(5) do
    DS:SetAsync(plr.userId.."_DS", currency.Value)
  end
end)

【问题讨论】:

    标签: roblox


    【解决方案1】:

    我会在玩家加入时从数据存储中加载值,然后在每次死亡时递增它并将其保存到数据存储中。

    注意,为了触发死亡,脚本使用Humanoid.Died 事件。

    local deathsStore = game:GetService("DataStoreService"):GetDataStore("Deaths")
    
    function playerAddedHandler(plr)  
        -- key that is used to store the death value
        local playerKey = "Player_" .. plr.UserId 
    
        -- Add leader board
        local leaderstats = Instance.new("Folder", plr)  
        leaderstats.Name = "leaderstats"
    
        -- Add "Deaths" column to leader board 
        local deaths = Instance.new("IntValue", leaderstats)
        deaths.Name = "Deaths"
    
        -- Load Deaths value from previous game
        deaths.Value = deathsStore:GetAsync(playerKey) 
    
        -- Hook up event handler that is triggered when character dies
        plr.CharacterAdded:Connect(function(char)
            char.Humanoid.Died:Connect(function()
    
                -- increment death value on the leader board
                plr.leaderstats.Deaths.Value = plr.leaderstats.Deaths.Value + 1
    
                -- save the value in our deaths datastore
                local success, err = pcall(function()
                    deathsStore:SetAsync(playerKey, plr.leaderstats.Deaths.Value)
                end)    
            end)
        end)
    end
    
    game.Players.PlayerAdded:Connect(playerAddedHandler) -- call function "playerAddedHandler" every time a player joins the game
    

    还有。不要忘记启用数据存储使用,否则会在输出中显示 403 错误。您可以在 roblox 网站上的游戏基本设置中执行此操作:

    【讨论】:

      猜你喜欢
      • 2017-06-19
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2010-12-15
      • 2010-10-07
      • 2011-04-15
      • 1970-01-01
      相关资源
      最近更新 更多