Saving timer on player leave?

Hello Roblox Dev Community! I have a request I hope some of you can fulfil with! You see, I’m trying to make it so when you click on a button, theres a cooldown before you can click it again. But the problem is, players can just rejoin and click it again. How would I make it so the cooldown saves whenever you leave?

Heres the code to the button that needs to be connected to the Datastore.

script.Parent.Parent.Parent.Parent.EasyMaze.MouseButton1Click:Connect(function()
	for i = 300,0,-1 do
		script.Parent.Text = i
		wait(1)

		if i == 0 then
			script.Parent.Parent.Parent.Visible = false
			script.Parent.Parent.Parent.Parent.EasyMaze.Visible = true
		end
	end
	end)

Use os.time to determine the duration since the last click.

1 Like

You forgot one more thing to add with it:

1 Like

Putting things together:

local store = game:GetService("DataStoreService"):GetDataStore("Example")
local players = game:GetService("Players")

local function save(player:Player)
    local key = "Prefix_"..player.UserId
    local success, result = pcall(store.UpdateAsync, store, key, function(old) return os.time() end)
    if not success then
        warn(result)
    end
end

local function load(player:Player)
    local key = "Prefix_"..player.UserId
    local success, data = pcall(store.GetAsync, store, key)
    if success then
        local current = os.time() - data
        print(current) --how long between when they left and they rejoined, in seconds
    else
        player:Kick("Failed to load data. Please rejoin!")
    end
end

players.PlayerAdded:Connect(load)
players.PlayerRemoving:Connect(save)
1 Like

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