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)
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)
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)