如何为超过 3 个统计数据的排行榜制作保存系统
How do I make a save system for a leader board with more than 3 stats
我想做一个存档系统,这样大家玩的时候就不用每次都重启
我真的不知道该怎么做,所以我会向您展示我的领导者统计数据的代码,该代码位于作品 space
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)
我又不知道该怎么做了,请帮忙。
这是一篇关于数据存储的更好的文章:(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。因此,请务必查看 link 中标题为:Using Data Stores in Studio.
的部分,它将引导您浏览菜单。
总之...
现在,您正在创建玩家加入游戏时的起始值。 DataStores 允许您保存上次 session 的值,然后在下次他们加入时加载这些值。
Roblox DataStores 允许您存储 key-value 表。让我们创建一个助手 object 来管理数据的加载和保存。
制作一个名为 PlayerDataStore 的模块脚本:
-- 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)
希望对您有所帮助!
我想做一个存档系统,这样大家玩的时候就不用每次都重启
我真的不知道该怎么做,所以我会向您展示我的领导者统计数据的代码,该代码位于作品 space
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)
我又不知道该怎么做了,请帮忙。
这是一篇关于数据存储的更好的文章:(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。因此,请务必查看 link 中标题为:Using Data Stores in Studio.
的部分,它将引导您浏览菜单。
总之...
现在,您正在创建玩家加入游戏时的起始值。 DataStores 允许您保存上次 session 的值,然后在下次他们加入时加载这些值。
Roblox DataStores 允许您存储 key-value 表。让我们创建一个助手 object 来管理数据的加载和保存。
制作一个名为 PlayerDataStore 的模块脚本:
-- 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)
希望对您有所帮助!