为什么硬币只在玩家第一次加入游戏时保存?
Why do the coins only save the first time a player joins the game?
所以我正在 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)
请帮忙,我真的很需要答案。
谢谢
这也发生在我身上,我认为你正在 Roblox Studio 中测试游戏。在 Studio 中,有时会报错。尝试发布游戏,然后检查它是否有效。
所以我正在 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)
请帮忙,我真的很需要答案。
谢谢
这也发生在我身上,我认为你正在 Roblox Studio 中测试游戏。在 Studio 中,有时会报错。尝试发布游戏,然后检查它是否有效。