lua - Roblox Lua 中的数据存储
问题描述
所以我目前正在尝试让保存脚本与鼠标单击事件游戏一起使用。它应该保存玩家的点击次数(点数)。问题是,它似乎根本没有节省。排行榜/鼠标点击事件有效,没有输出/脚本分析错误,脚本在ServerScriptService中。
这是代码:
local DataStoreService = game:GetService("DataStoreService")
classData = DataStoreService:GetDataStore("PlayClass")
pointData = DataStoreService:GetDataStore("PlayPoints")
game.Players.PlayerAdded:Connect(function(player)
local success, err = pcall(function()
pointData:GetAsync("Player_"..player.UserId)
end
)
if success then
print("Loading Success!")
end
local success, err = pcall(function()
classData:GetAsync("Player_"..player.UserId)
end
)
if success then
print("Loading Success!")
end
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Points = Instance.new("IntValue", leaderstats)
Points.Name = "Points"
local Class = Instance.new("StringValue", leaderstats)
Class.Name = "Class"
Class.Value = "Wannabe"
PlayerClass = player.leaderstats.Class
PlayerPoints = player.leaderstats.Points
end)
workspace:WaitForChild("Sign")
workspace.Part.ClickDetector.MouseClick:Connect(function(player)
workspace.Sign.Value.Value = workspace.Sign.Value.Value + 1
workspace.Sign.SurfaceGui.SIGN.Text = workspace.Sign.Value.Value
PlayerPoints.Value = workspace.Sign.Value.Value
if PlayerPoints.Value < 49
then
PlayerClass.Value = "Wannabe"
end
if PlayerPoints.Value > 49
then
PlayerClass.Value = "Beginner"
end
if PlayerPoints.Value > 124
then
PlayerClass.Value = "Novice"
end
if PlayerPoints.Value > 249
then
PlayerClass.Value = "Intermediate"
end
if PlayerPoints.Value > 374
then
PlayerClass.Value = "Pro"
end
if PlayerPoints.Value > 499
then
PlayerClass.Value = "God"
end
end
)
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
pointData:SetAsync("Player_"..player.UserId, PlayerPoints.Value)
end
)
if success then
print ("Saving Success!")
end
local success, err = pcall(function()
classData:SetAsync("Player_"..player.UserId, PlayerClass.Value)
end
)
if success then
print("Saving Success!")
end
end)
它打印加载成功,但不打印保存成功。编辑:我尝试添加一个 else 语句来查看错误,但它仍然没有打印任何内容。
解决方案
我可以使用空白游戏、零件和脚本来复制您的问题。正如 Kylaaa 在他的评论中提到的,如果你这样做:
if success then
print ("Saving Success!")
else
print (err)
end
...它会告诉你错误。如果您尝试过这种方式,我不确定您的评论。此外,我每次输出时都没有收到错误。在我的情况下,错误是:“403:如果未启用 API 访问,则无法从工作室写入 DataStore。” 启用后,“保存成功”显示得很好。
推荐阅读
- kubernetes - 删除应用 Helm2.13.1 创建的所有 Pod
- r - ggplot geom_bar 按组为 0 值留下空格
- sql - 使用分析函数时的分组
- python - 捕获图像并提交给kivy python中的其他功能
- javascript - select2(药盒)和引导主题的问题
- javascript - Javascript Modules SyntaxError:不能在模块外使用 import 语句
- jupyter-lab - Ipywidget“选择”不会触发鼠标选择回调
- c# - Moq It.IsAnyType 不适用于具有泛型类型的 Func 返回任务
- c++ - C++ 中执行“mkdir -p”的现代方式是什么?
- reactjs - Axios 与 ReactJS 中的 Fetch