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)
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))
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.
--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)
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?
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)
--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)