首页 > 解决方案 > 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 语句来查看错误,但它仍然没有打印任何内容。

标签: luaroblox

解决方案


我可以使用空白游戏、零件和脚本来复制您的问题。正如 Kylaaa 在他的评论中提到的,如果你这样做:

if success then
    print ("Saving Success!")
else
    print (err)
end 

...它会告诉你错误。如果您尝试过这种方式,我不确定您的评论。此外,我每次输出时都没有收到错误。在我的情况下,错误是:“403:如果未启用 API 访问,则无法从工作室写入 DataStore。” 启用后,“保存成功”显示得很好。


推荐阅读