我想创建一个保存系统,以使人们不必每次玩都重新启动
我真的不知道该怎么做,因此我将向您展示位于工作空间中的领导者统计代码
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)
我还是不知道该怎么做,请帮忙。
答案 0 :(得分:0)
对于Data Stores((https://developer.roblox.com/articles/Data-store)来说,这是更好的文章。测试的重要警告:127.0.0.1
因此,您将必须发布游戏并对其进行在线配置,以允许您发出HTTP请求并访问数据存储API。因此,请务必查看链接中标题为DataStoreService cannot be used in Studio if a game is not configured to allow access to API services.
的部分。
无论如何...
现在,您正在创建玩家加入游戏时的起始值。通过数据存储,您可以保存上一个会话中的值,然后在下次连接时加载这些值。
Roblox数据存储库允许您存储键值表。让我们创建一个帮助对象来管理数据的加载和保存。
制作一个名为PlayerDataStore的ModuleScript:
Using Data Stores in Studio.
现在,我们可以使用此模块来处理所有加载和保存操作。
最终,您的播放器加入代码将与您的示例非常相似,它将尝试首先加载数据。收听播放器何时离开也很重要,这样您就可以保存下一次的数据。
在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
希望这会有所帮助!