Time Button System

What would I like to achieve?
Hi there! I’ve got a question, I’m working on a system that once you click on a button, you have to wait 8 hours until you can click the button again. It has also to be that if you leave the game, it still goes on and the time doesn’t only go down when your in the game.

What have I tried so far?
I have tried using os.time and datastores, but this is the first time I really used os.time and it didn’t work the way I did it.

Alright, so, reopened, I tried this, but it didn’t work. It’s a server script.

local MainDataStore = game:GetService("DataStoreService"):GetDataStore("MainDataStore")

game.Players.PlayerAdded:Connect(function(player)
	
	local clicked = Instance.new("BoolValue")
	clicked.Name = "Clicked"
	clicked.Value = MainDataStore:GetAsync(player.UserId.."-clicked")
	clicked.Parent = player
	
	local timeSiceLastClicked = Instance.new("NumberValue")
	timeSiceLastClicked.Name = "TimeSiceLastClicked"
	timeSiceLastClicked.Value = MainDataStore:GetAsync(player.UserId.."-time")
	timeSiceLastClicked.Parent = player
	
end)

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
	
	local currentTime = os.time()

	MainDataStore:GetAsync(player.UserId.."-time")

	local timeSinceClicked = (currentTime - player.TimeSiceLastClicked.Value)
		
	if timeSinceClicked >= 60 then
		player.Clicked.Value = true
		player.TimeSiceLastClicked.Value = os.time()
	end
	
	wait(5)
	
	player.Clicked.Value = false

end)

game.Players.PlayerRemoving:Connect(function(player)
	MainDataStore:SetAsync(player.UserId.."-clicked", player.Clicked.Value)
	MainDataStore:SetAsync(player.UserId.."-time", player.TimeSiceLastClicked.Value)
end)

Thanks for reading and I hope you can help me :slightly_smiling_face:

I can say only that you should use os.time to achieve this .

1 Like

What did you try to store in os.time?

And also the way i do it is by making a number value with a value at which timestamp the player first claimed the chest and then store its data and after 8 hours and the player claims the daily reward the number value will update its value to the timestamp that the player claimed the chest

And as for the time left text thing you will need to substract the next 8 hours of the timestamp value that the player currently has by the the timestamp value that the player currently has

So it’s basically like for example 1600028800 - 1600000000 = 28800 seconds remaining ((currenttimestamp+28800) - timestampwheretheplayerlastclaimedthechest = ?) and then use print(os.date(“%X”,variableofhowmanysecondsremaining))

1 Like

You can store the time of the click in a variable using time(), and have the datastore save the elapsed time with time() - variable. The next time the player joins the game, the datastore would subtract the elapsed time (which was saved) from 8x60x60 (8 hours) and this would be the new condition.

1 Like

Thanks for reacting! I have an online class right now, I will look into it soon.

Alright! I’m only not sure how to implement this right now.

It basically works like this
Leaderstats script:

--Get data store service variables here
game.Players.PlayerAdded:Connect(function(plr)
         --Insert leaderstats stuff here
         local secretfolder = Instance.new("Folder",plr) --For safety reasons i guess
         secretfolder.Name = "secretFolder"
     
         local lastclaimed = Instance.new("NumberValue",secretfolder)
         lastclaimed.Name = "lastClaimed"
         --Store data here with the last claimed but if you don't know how to store data outside of the leaderstats then don't make the secret folder and put the lastClaimed value thing in the leaderstats folder
end)

I know how to do this, but not the other part.

This video might help you;

It’s probably pretty similar to what you want to achieve.

Basically you have to use os.time and subtract the current os.time with the saved os.time and then divide it by 3600 so you get hours instead of seconds. Then you simply check if that is greater than 8 hours and if so, give him something, I guess?

1 Like
local clickTime
local debounce = 8*60*60
local elapsedTime

-- DataStore updates clickTime: clickTime = time() - savedElapsedTime
-- DataStore also saves the elapsed time upon leaving: savedElapsedTime = time() - clickTime

Button.MouseButton1Click:Connect(function()
    if clickTime ~= nil then
        elapsedTime = time()-clickTime
        if elapsedTime < debounce then
            return true
        end
    else
        clickTime = time()
        do
            -- Event goes here
        end
    end
end)
3 Likes

Yeah, that’s what I tried first, but it didn’t work for me when I tried to implement it.

1 Like

Touched script:

--add a claim function here
game.Workspace.ClaimChestPartThingy.Touched:Connect(function(hit) --Obviously makes a touched function
     local plr = game.Players:GetPlayerFromCharacter(hit)
     if plr.secretFolder.lastClaimed.Value == 0 or (plr.secretFolder.lastClaimed.Value - os.time()) >= 28800 then --Checks if the player is either new or has last claimed the chest 8 hours ago
        claim()
end
end)
1 Like

Thanks a lot! That was where I was searching for.

Your welcome and i also fixed the mistake when assigning the plr variable since i forgot which character to get player

Doesn’t matter, I only had to know the os.time part.

Just keep in mind that this is only for the chest system so it won’t really work for the billboard gui text thing

I do, I know how to implement it into other things.