I’m currently making activity logs in my Discord server, to where when a Player gets added into the game, a timer will start. When the player gets removed, I need a time of how long that player was in the game. e.g “30 minutes” or “2 hours & 3 minutes”.
I really don’t know how to start or any way to start other than make a Player added event and a Player removing event. Help would be appreciated, thank you.
(I don’t need the code to where it needs to be sent to the Discord. I just need to find a way to find a time of how long the player was in-game when they leave.)
1 Like
You can make a leaderstats that goes up in value by 1 every 60 seconds and name it minutes.
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "Leaderstats"
local Minutes = Instance.new("IntValue", leaderstats)
Minutes.Name = "Minutes"
Minutes.Value = 0
while true do
wait (60)
Minutes.Value = Minutes.Value + 1
end
end)
1 Like
You don’t need leaderstats for this, just store it in a table. Mark down when the player joins and when the player leaves. Then find the difference between those two.
local PlayerTimes = {}
game.Players.PlayerAdded:Connect(function(plr)
PlayerTimes[plr] = tick()
end)
game.Players.PlayerRemoving:Connect(function(plr)
if PlayerTimes[plr] then
print(tick() - PlayerTimes[plr])
-- do stuff
end
PlayerTimes[plr] = nil
end)
tick()
gives a time value in seconds (specifically the time elapsed since January 1, 1970). So by subtracting the time when the player joins from the time when the player leaves, you get the amount of seconds the player was in the game. Then you can convert that to minutes/hours/etc.
5 Likes
not that @dibblydubblydoo’s code wasn’t fine. you should probably consider using @astrocode’s.
storing variables inside of the player is exploitable.
2 Likes