Time 将当前播放时间添加到总播放时间

Time 将当前播放时间添加到总播放时间,time,lua,Time,Lua,我有一个以秒为单位的总游戏时间数据库。我想从数据库中获取这些秒数,以秒为单位添加当前会话播放时间,然后更新数据库。 这应该每5秒发生一次。我已经这样做了,但是因为我做了currentSession+totalTimePlayedDB它不断地添加我当前会话的完整持续时间。。。有什么想法吗 local currentPlayTime = player:TimeConnected() print(math.Round(currentPlayTime)) local playerValues = MyS

我有一个以秒为单位的总游戏时间数据库。我想从数据库中获取这些秒数,以秒为单位添加当前会话播放时间,然后更新数据库。 这应该每5秒发生一次。我已经这样做了,但是因为我做了
currentSession+totalTimePlayedDB
它不断地添加我当前会话的完整持续时间。。。有什么想法吗

local currentPlayTime = player:TimeConnected()
print(math.Round(currentPlayTime))
local playerValues = MySQLite.queryValue([[SELECT time FROM chiz_time WHERE sid=']].. player:SteamID() ..[[']], function(time)
    if time == "" then 
        time = math.Round(currentPlayTime)

        else
            time = math.Round(time + time - currentPlayTime )
    end

    MySQLite.query([[UPDATE chiz_time SET time = ']].. time ..[[' WHERE sid=']].. player:SteamID() ..[[']])     
end)
我执行
currentSession
+
totalTimePlayedDB
它不断添加当前会话的完整持续时间

您只需要根据上次节省的时间计算增量

在初始化代码中的某个地方:

lastSaveTime = 0
在保存例程中:

totalTimePlayedDB = totalTimePlayedDB + currentSession - lastSaveTime
if (totalTimePlayedDB is written to the database successfully) then
   lastSaveTime = currentSession
end

密码?我读了两遍,仍然不明白你在做什么。每5秒更新一次DB值似乎效率很低。你不能简单地更新一次玩的总时间吗,比如当玩家断开连接时,游戏就结束了,等等?