【问题标题】:How do I make a save system for a leader board with more than 3 stats如何为超过 3 个统计数据的排行榜创建保存系统
【发布时间】:2019-10-09 10:42:39
【问题描述】:

我想制作一个存档系统,这样人们就不必每次玩都重新启动

我真的不知道该怎么做,所以我将向您展示我的领导者统计数据的代码,它位于工作空间中

local function onPlayerJoin(player)
    local leaderstats = Instance.new("Model")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player


    local gold = Instance.new("IntValue")
    gold.Name = "JumpBoost"
    gold.Value = 150
    gold.Parent = leaderstats

        local speed = Instance.new("IntValue")
    speed.Name = "Speed"
    speed.Value = 20
    speed.Parent = leaderstats

    local coin = Instance.new("IntValue")
    coin.Name = "CloudCoins"
    coin.Value = 0
    coin.Parent = leaderstats

    local rebirths = Instance.new("IntValue")
    rebirths.Name = "Rebirths"
    rebirths.Value = 0
    rebirths.Parent = leaderstats

end


game.Players.PlayerAdded:Connect(onPlayerJoin)

再次,我真的不知道该怎么做,请帮忙。

【问题讨论】:

  • 我已经尝试过在线教程,但似乎都不起作用
  • 您看过数据存储教程了吗? (developer.roblox.com/articles/Saving-Player-Data)
  • 不,我现在就这样做
  • 我刚刚做了,我真的不明白他们说了什么以及用给定的代码要改变什么。
  • 我也刚刚意识到我想说的第一条评论。

标签: scripting roblox


【解决方案1】:

这是一篇更适合数据存储的文章:(https://developer.roblox.com/articles/Data-store)。一个重要的测试警告:DataStoreService cannot be used in Studio if a game is not configured to allow access to API services. 所以你必须发布游戏并在线配置它以允许你发出 HTTP 请求并访问数据存储 API。因此,请务必查看该链接中标题为:Using Data Stores in Studio. 的部分,它将引导您浏览菜单。

无论如何...

现在,您正在创建玩家加入游戏时的初始值。数据存储允许您保存上次会话中的值,然后在下次加入时加载这些值。

Roblox 数据存储允许您存储键值表。让我们创建一个辅助对象来管理数据的加载和保存。

制作一个名为 PlayerDataStore 的 ModuleScript :

 -- Make a database called PlayerExperience, we will store all of our data here
 local DataStoreService = game:GetService("DataStoreService")
 local playerStore = DataStoreService:GetDataStore("PlayerExperience")

 local defaultData = {
    gold = 150,
    speed = 0,
    coins = 0,
    rebirths = 0,
}
local PlayerDataStore = {}

function PlayerDataStore.getDataForPlayer(player)
    -- attempt to get the data for a player
    local playerData
    local success, err = pcall(function()
        playerData = playerStore:GetAsync(player.UserId)
    end)

    -- if it fails, there are two possibilities:
    --  a) the player has never played before
    --  b) the network request failed for some reason
    -- either way, give them the default data
    if not success or not playerData then
        print("Failed to fetch data for ", player.Name, " with error ", err)
        playerData = defaultData
    else
        print("Found data : ", playerData)
    end

    -- give the data back to the caller
    return playerData
end

function PlayerDataStore.saveDataForPlayer(player, saveData)
    -- since this call is asyncronous, it's possible that it could fail, so pcall it
    local success, err = pcall(function()
        -- use the player's UserId as the key to store data
        playerStore:SetAsync(player.UserId, saveData)
    end)
    if not success then
       print("Something went wrong, losing player data...")
       print(err)
    end
end


return PlayerDataStore

现在我们可以使用这个模块来处理我们所有的加载和保存。

最终,您的玩家加入代码将与您的示例非常相似,它只是尝试首先加载数据。聆听玩家何时离开也很重要,这样您就可以保存他们的数据以备下次使用。

在 PlayerDataStore 旁边的脚本中:

-- load in the PlayerDataStore module
local playerDataStore = require(script.Parent.PlayerDataStore)


local function onPlayerJoin(player)
    -- get the player's information from the data store,
    --  and use it to initialize the leaderstats
    local loadedData = playerDataStore.getDataForPlayer(player)

    -- make the leaderboard
    local leaderstats = Instance.new("Model")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local gold = Instance.new("IntValue")
    gold.Name = "JumpBoost"
    gold.Value = loadedData.gold
    gold.Parent = leaderstats

    local speed = Instance.new("IntValue")
    speed.Name = "Speed"
    speed.Value = loadedData.speed
    speed.Parent = leaderstats

    local coin = Instance.new("IntValue")
    coin.Name = "CloudCoins"
    coin.Value = loadedData.coins
    coin.Parent = leaderstats

    local rebirths = Instance.new("IntValue")
    rebirths.Name = "Rebirths"
    rebirths.Value = loadedData.rebirths
    rebirths.Parent = leaderstats
end

local function onPlayerExit(player)
    -- when a player leaves, save their data
    local playerStats = player:FindFirstChild("leaderstats")
    local saveData = {
        gold = playerStats.JumpBoost.Value,
        speed = playerStats.Speed.Value,
        coins = playerStats.CloudCoins.Value,
        rebirths = playerStats.Rebirths.Value,
    }
    playerDataStore.saveDataForPlayer(player, saveData)
end


game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

希望这会有所帮助!

【讨论】:

  • 感谢您的帮助,我对玩家数据保存有了更好的了解
  • 酷,如果它解决了你的问题,你能接受答案吗?
  • 是的,我绝对可以做到。
  • Umm 它停止工作,并且在模块脚本第 24 行的 if 下它说它是预期的)而不是 if。
  • 哦,哎呀,我明白我的错误了。我忘了关闭 pcall()。在 getDataForPlayer 中。马上更新
猜你喜欢
  • 1970-01-01
  • 2021-10-07
  • 2023-03-30
  • 1970-01-01
  • 2019-10-22
  • 1970-01-01
  • 1970-01-01
  • 2020-12-25
  • 2017-03-13
相关资源
最近更新 更多