【问题标题】:My data store system isn't working, thought I get a successfully saved message. Roblox我的数据存储系统不工作,以为我收到了成功保存的消息。罗布洛克斯
【发布时间】:2021-04-26 18:47:41
【问题描述】:

所以我制作了一个数据存储系统来拯救 Silver。我使用了 pcalls,每当玩家离开时,我要么没有收到任何消息,要么只是成功保存,奇怪的是我从来没有收到任何错误。

但它不起作用。我尝试在 Roblox 本身中进行,而不仅仅是在 Studio 中。不工作。这是 ServerScriptService 中的服务器脚本。

请帮忙:D

local dataStore = game:GetService("DataStoreService"):GetDataStore("myDataStore")


game.Players.PlayerAdded:Connect(function(plr)
    
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr
    
    local silver = Instance.new("IntValue")
    silver.Name = "Silver"
    silver.Parent = leaderstats
    
    local playerUserId = "Player_"..plr.UserId
    
    local data
    local success, errormessage = pcall(function()
        data = dataStore:GetAsync(playerUserId)
    end)
    
    if success then
        silver.Value = data
    end
    

end)


game.Players.PlayerRemoving:Connect(function(plr)
    local playerUserId = "Player_"..plr.UserId
    
    local data = plr.leaderstats.Silver.Value
    
    local success, errormessage = pcall(function()
        dataStore:SetAsync(playerUserId, data)
    end)
    
    if success then
        print("Data successfully saved.")
    else
        print("Error when saving data.")
        warn(errormessage)
    end
end)```

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    数据存储需要一个字符串作为键。您将一个整数传递给 SetAsync,您需要使用 tostring() 函数将其转换为字符串。

    您更正后的代码应如下所示。

    local dataStore = game:GetService("DataStoreService"):GetDataStore("myDataStore")
    
    
    game.Players.PlayerAdded:Connect(function(plr)
        
        local leaderstats = Instance.new("Folder")
        leaderstats.Name = "leaderstats"
        leaderstats.Parent = plr
        
        local silver = Instance.new("IntValue")
        silver.Name = "Silver"
        silver.Parent = leaderstats
        
        local playerUserId = "Player_"..plr.UserId
        
        local data
        local success, errormessage = pcall(function()
            data = dataStore:GetAsync(tostring(playerUserId))
        end)
        
        if success then
            silver.Value = data
        end
        
    
    end)
    
    
    game.Players.PlayerRemoving:Connect(function(plr)
        local playerUserId = "Player_"..plr.UserId
        
        local data = plr.leaderstats.Silver.Value
        
        local success, errormessage = pcall(function()
            dataStore:SetAsync(tostring(playerUserId), data)
        end)
        
        if success then
            print("Data successfully saved.")
        else
            print("Error when saving data.")
            warn(errormessage)
        end
    end)
    

    【讨论】:

      猜你喜欢
      • 2021-02-02
      • 1970-01-01
      • 2022-01-23
      • 2021-12-03
      • 2018-07-31
      • 1970-01-01
      • 2023-03-17
      • 2022-01-05
      • 2020-09-05
      相关资源
      最近更新 更多