我无法在 roblox 游戏中保存数据。
local players = game:GetService("Players")
local dss = game:GetService("DataStoreService")
local gameData = dss:GetDataStore("Nara")
players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
local success, result = pcall(function()
local r = gameData:GetAsync(player.UserId)["Coins"]
return r
end)
if success then
print("success: " .. result)
coins.Value = result
else
print(result)
end
end)
players.PlayerRemoving:Connect(function(player)
print("player removed")
local key = player.UserId
local data = {
["Coins"] = player:WaitForChild("leaderstats").Coins.Value
}
local success, err = pcall(function()
gameData:SetAsync(key, data)
end)
if success then
print("Data saved successfully")
else
print("Failed to save data with error: ", err)
end
end)
game.ReplicatedStorage.Buy.OnServerInvoke = function(player, item)
if not player:FindFirstChild(item) then
if player.leaderstats.Coins.Value >= game.ReplicatedStorage.Items:WaitForChild(item).cost.Value then
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - game.ReplicatedStorage.Items:WaitForChild(item).cost.Value
local owned = Instance.new("BoolValue")
owned.Name = item
owned.Parent = player
player.Backpack:ClearAllChildren()
game.ReplicatedStorage.Items:WaitForChild(item):Clone().Parent = player.Backpack
return item
else
return "not enough coins"
end
else
player.Backpack:ClearAllChildren()
game.ReplicatedStorage.Items:WaitForChild(item):Clone().Parent = player.Backpack
return "owned " .. item
end
end
我尝试了更多方法,但我不知道出了什么问题。
我期望在玩家加入时保存然后加载硬币 IntValue。我尝试了一切,但没有让它发挥作用。价值是被创造出来的,没有任何东西会破坏它。代码在 pcall 保存时卡住了,尝试使用
print
看看哪里。谢谢
您是否启用了工作室访问权限,如果没有,您可以阅读有关如何启用它的文档:https://create.roblox.com/docs/cloud-services/datastores