How would I check how long a player has been offline?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    A tool spawner that lets u spawn a tool every 60 seconds, I have this, but I do not want to let people just server hop to escape the cooldown
  2. What is the issue? Include screenshots / videos if possible!
    I don’t know how to do this
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I cant find any
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
-- This is an example Lua code block

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

What you can do is when the player leaves the game it will save a time to their player. When the player joins back you can do math and calculate how long it has been since they left and joined.

1 Like

do you know what the method would be to find the time they leave?

You’ll have to use save the player’s data with an entry for ‘timeLeft’.

You can set this to os.time(), and then compare the time they join a server to the timeLeft data.

os.time() - timeLeft -- time passed

You’ll also have to account for how much time on the cooldown they have left. For example, if they leave with 30 seconds left on cooldown.

1 Like

Super simple, you will need to store the time at which they left and read it when they rejoin. This will obviously utilize a DataStore. In practice, the most utilized DataStore module is ProfileService, but assuming you’re just using the default, built-in DataStoreService: Your code may end up looking like this.

-- server
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local datastores = {}

Players.PlayerAdded:Connect(function(player)
    local userData = DataStoreService:GetDataStore("UserData", player.UserId)
    datastores[player.UserId] = userData

    local timePlayerLeft = userData:GetAsync("timeLeft") or os.time()
    local timePassed = os.time() - timePlayerLeft

    -- do whatever with that
end)

Players.PlayerRemoving:Connect(function(player)
    if not datastores[player.UserId] then
        return
    end

    local timePlayerLeft = os.time()

    datastores[player.UserId]:SetAsync("timeLeft", timePlayerLeft)
    datastores[player.UserId] = nil
end)

Obviously, this is a very crude example of how your datastore code may end up looking. But the example is there.

1 Like

Thanks a lot! I didn’t know how to use os.time() that much until now