【发布时间】:2020-07-19 22:41:55
【问题描述】:
所以我在 Roblox 中制作了一个 obby。我有一个数据存储来保存排行榜值,即硬币和检查点。我拥有的数据存储在 ServerScriptService 中。
问题是当玩家第一次加入游戏时,例如他们完成了游戏的前 5 个关卡,他们会得到 2 个硬币。下次他们加入游戏时,他们仍然处于第 5 级,并且他们仍然有 2 个上一场比赛的硬币。但是如果他们现在玩游戏(即他们第二次登录游戏)并且假设他们达到了 10 级并且有 4 个硬币,那么如果玩家现在离开游戏并且当他们重新加入时,他们的过程应该是这样的应该在 10 级并且应该有 4 个硬币。
但是发生的情况是,他们不在 10 级并且他们没有 4 个硬币,他们仍然在 5 级并且他们的硬币是 2。就像他们在第一次游戏时的进度一样。
为什么当玩家第三次登录游戏时,此数据存储不更新排行榜上的值?等等?
这是包含位于 ServerScriptService 中的整个数据存储的脚本:
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Checkpoint = Instance.new("IntValue", leaderstats)
Checkpoint.Name = "Checkpoint"
Checkpoint.Value = 1
local coins = Instance.new("IntValue", leaderstats)
coins.Name = "Coins"
coins.Value = 0
--Checkpoint Section
player.CharacterAdded:Connect(function(character)
repeat wait() until player.character ~= nil
local checkpoint = game.Workspace.Checkpoints:FindFirstChild(Checkpoint.Value)
character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(checkpoint.Position +
Vector3.new(0, 2, 0))
end)
-- Data Store Section
local playerUserId = "Player_"..player.UserId
print(playerUserId)
-- Load Data
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
end)
if success then
if data then
coins.Value = data.Coins
Checkpoint.Value = data.Checkpoint
-- Set our data equal to the current Coins
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local data = {
Coins = player.leaderstats.Coins.Value;
Checkpoint = player.leaderstats.Checkpoint.Value;
}
local success, errormessage = pcall(function()
myDataStore:SetAsync(playerUserId, data)
end)
if success then
print("Data successfully saved!")
else
print("There was an error saving the data!")
warn(errormessage)
end
end)
请帮忙,因为我真的需要答案。
谢谢
【问题讨论】: