Roblox 中的数据存储 Lua
DataStore in Roblox Lua
所以我目前正在尝试获取一个保存脚本来处理鼠标单击事件游戏。它应该保存玩家的点击次数(点数)。问题是,它似乎根本没有保存。 leaderboard/mouse点击事件有效,没有output/script分析错误,脚本在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。”启用后,"Saving Success" 显示正常。
所以我目前正在尝试获取一个保存脚本来处理鼠标单击事件游戏。它应该保存玩家的点击次数(点数)。问题是,它似乎根本没有保存。 leaderboard/mouse点击事件有效,没有output/script分析错误,脚本在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。”启用后,"Saving Success" 显示正常。