Get player's current playtime

How can I find out how long the player has been playing the game?

1 Like

In the session or their total time?

1 Like

I want the current session, not the total

1 Like

add this server script in serverscriptservice

local times = {}

function playerAdded(player)
    if times[player.UserId] then return end
    times[player.UserId] = 0
    while task.wait(1) do
        if not times[player.UserId] then break end
        times[player.UserId] += 1
    end
end

for _, v in game.Players:GetPlayers() do
    task.spawn(playerAdded, player)
end

game.Players.PlayerAdded:Connect(function(player)
    task.spawn(playerAdded, player)
end)

game.Players.PlayerRemoving:Connect(function(player)
    if not times[player.UserId] then return end
    times[player.UserId] = nil
end)
1 Like

There are several mistakes in your code,

You cannot directly execute both break and return, and you’re calling the task.spawn() even before the function is declared, but thanks anyways, I’ll give it a try

if that’s the case, remove ‘return’. I’m not at my pc

I’m pretty sure you can call functions before they are defined

Might be task.spawn() then

Well, on the other hand, it works perfectly, thanks!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.