Help with OS Time

I want to achieve: I am trying to make a time that gets the time that between a player leaving and rejoining

The issue: I can’t find any post that can help me solve this

I have you tried so far: check issue also checked roblox developer API

I would like the data to be formated in too var hour and minutes but not sure how

game.Players.PlayerAdded:Connect(function(plr)
         -- Data store stuff
        local idleTime = leaveTime - os.time()

        --???
end)

game.Players.PlayerRemoving:Connect(function(plr)
         local leaveTime = os.time()
         -- Data store stuff
end)

well after a bit of searching os.date might be what your looking for

local startTime
game.Players.PlayerAdded:Connect(function(plr)
    startTime = os.time()
end)
game.Players.PlayerRemoving:Connect(function(plr)
   local total =  os.date("%X",os.difftime(startTime,os.time()))
end)

tell me if this would work

1 Like

But I want to measure from the player leaving to them rejoining

Since you want to find the difference in the time, you could store the time in a Datastore since os.time returns an integer representing the seconds from the Unix Epoch. Your code could look something like this:

local Datastore = game:GetService("DataStoreService"):GetDataStore("time")

game.Players.PlayerAdded:Connect(function(Player)
     local Data = Datastore:GetAsync(Player.UserId.."Time") or os.time()
     local lastTimeSeen = (os.time()-Data) --this represents the difference in time (in seconds) from the last join period
end)

game.Players.PlayerRemoving:Connect(function(Player)
    local Success, err = pcall(function()
        Datastore:SetAsync(Player.UserId.."Time", os.time())
    end)
end)
2 Likes

how would I put that data into a hours and a minutes varible?

well for that my formatting part should work,

local lastTimeSeen = os.date("%X",os.difftime(Data,os.time()))
1 Like
local Data = {}

game.Players.PlayerAdded:Connect(function(Player)
	Data[Player.UserId] = tick()
end)
game.Players.PlayerRemoving:Connect(function(Player)
	local TimeSpent = math.floor(tick() - Data[Player.UserId])
	print(Player.Name.." is leaving after playing for "..string.format("%02iH:%02iM:%02iS", TimeSpent/60^2, TimeSpent/60%60, TimeSpent%60))
	Data[Player.UserId] = nil
end)

You can use simple math to convert seconds into minutes and seconds into hours:

Assuming that 1 minute contains 60 seconds, you can divide the number of seconds by 60 to get your current minute.

Assuming that there are 3,600 seconds in an hour, you can divide the number of seconds by 3,600 to get your current hours.

I am getting this error
Screen Shot 2021-02-11 at 09.47.11

You need to turn on your studio access to API services.

1 Like