这是一篇更适合数据存储的文章:(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)
希望这会有所帮助!