Hry all,
i have a button that i want people to be clickable once every 24 hours.
so you can click it, it stores the value in an intvalue, then after 24 hours you can click the button again and it will update the time you last clicks the button, so you cant click it for another 24 hours, however it doesnt work and just says “hasent been 24 hours yet” (i added a print to see what was happening)
Any help will be appreciated:
local RS = game.ReplicatedStorage
local TS = game:GetService(“TweenService”)
local Modules = game.ServerScriptService:WaitForChild(“Modules”)
local EggHandler = require(Modules:WaitForChild(“EggServerHandler”))
local function misc(plr, Type, Name)
if not plr or not Type or not Name then return end
EggHandler:OpenSpecialEgg(plr, Type, “Buy1”)
return false
end
local function toHMS(s) --convert to hours, minutes, seconds and format to 0 left-padded integers separated by colons.
return (“%02i:%02i:%02i”):format(s/3600, s/60%60, s%60)
end
– Place inside the Part
script.Parent.ClickDetector.MouseClick:Connect(function(plr)
local player = plr
local CD = plr.Data.MazeCD.Value
if CD == os.time() + 5 then
misc(plr, “Maze Egg”, ‘’)
CD = os.time()
else
print(“Hasent been 24 hours yet”)
print(toHMS(CD))
end
–plr.Data.MazeCD.Value = os.time()
end)
Thanks
Probably because you’re checking for equality rather than equality or greater than, try this?
local RS = game.ReplicatedStorage
local TS = game:GetService(“TweenService”)
local Modules = game.ServerScriptService:WaitForChild(“Modules”)
local EggHandler = require(Modules:WaitForChild(“EggServerHandler”))
local function misc(plr, Type, Name)
if not plr or not Type or not Name then return end
EggHandler:OpenSpecialEgg(plr, Type, “Buy1”)
return false
end
local function toHMS(s) --convert to hours, minutes, seconds and format to 0 left-padded integers separated by colons.
return ("%02i:%02i:%02i"):format(s/3600, s/60%60, s%60)
end
–-Place inside the Part
script.Parent.ClickDetector.MouseClick:Connect(function(plr)
local player = plr
local CD = plr.Data.MazeCD.Value
if CD >= (os.time() + 5) then
misc(plr, "Maze Egg", '')
CD = os.time()
else
print("Hasent been 24 hours yet")
print(toHMS(CD))
end
plr.Data.MazeCD.Value = os.time()
end)
I didn’t know if you wanted the last line to be commented or not so I uncommented it
i only commented it because it was meant to set it to the new time after they had claimed it but it didnt update it so i commented it out, thanks.
Will try it now and let you know
Thanks
Thanks for the reply, this still didnt work, as it came up with the same print (hasent been 24 hours)
Im going to look at doing it another way and having a if canclick == true then, and then have it run instead.
im new to using os.time and that format doesnt work, it just prints out 448984:19:57
so doesnt really tell me what time it was set.
so its hard to work out when i need the next os.time to check against.
Hmm, first thing I would recommend is when press it, check what is in CD and the result of os.time() + 5 to see what is given after waiting a few seconds?
yeah i tried that, i only set it to os +5 to see if it would wait 5 seconds rather than the full 24 hours.
im going to try and set the cooldown (CD) to os.time + 86,400 and then if the os.time is less than CD you wont be able to click the button, then if its more you will be able to click it, and it will update the CD value to os.time +86,400 again and see if that works?
When you printed both of those? What did you get as a result after 5 seconds?
Also you can reply to my messages so I will be able to know when you replied
oh yeah sorry. new to the forums.
the time was about 8 seconds off, it printed the os time and then the CD and it was about 7 seconds apart
Oh wait I see why it will never work. I think you need to use <=
rather than >=
, because CD is always going to be the lowest. Maybe here
if CD >= (os.time() + 5) then
Try changign it to
if (CD + 5) <= os.time() then
So if it’s less than the current time plus 5, it means over 5 seconds have passed so far
Say CD is 10 so far and os.time is 11. If we did 11+5, it’s going to be 16 so CD is always going to be the lower value, so it’s alway sgoing to say that, whereas if we did
CD + 5 it is 15, so it’s greater than os.time, so in this case, we check if CD + 5 is less than os.time to check if 5 seconds have passed in this case
oh yeah that makes sense,
however i did change it and just increased the time from 5 to 25 and it worked the first time but now it just says “hasent been 24 hours yet” but prints off what os.time is.
then it works sometime later.
so it worked and i pressed it right away and it said 21:19 but then wouldnt work again untill 22:06.
Maybe its just me not understanding how os.time works, i might be able to make it work if i do it the if true way. i will test it and post the results.
Did you remember to uncomment out that line at the end for setting the value to os.time()? Or wait, maybe the if statement you cna do
if (os.time() - CD) >= 5 then
To get the difference
yeah i did but that was wrong anyway as i want to set the CD to os.time + 86400.
Yeah that might work? CD would be the value set the first time it was clicked though so would os - CD be greater?
It would be a positive value since CD is the first value so it would be lower than os.time()
ok well ill rewrite it and chane it to set the CD value to os.time+30 (just so i can test it) and see if it works.
then will do a canclick == true then run script
if its not true then ill print a count down or something so i can see if its atleast setting the right time, then i can set it to 86400 if it works then it should be 24 hours lol.
Thanks so much for your help! very much appreciated
1 Like
Alright, I wish you good luck! If you have anymore issues, I’ll be here to help!
Thank you!
Quick one, does this seem right to you?
script.Parent.ClickDetector.MouseClick:Connect(function(plr)
local canclick = false
local CD = plr.Data.MazeCD.Value + 86400
if CD <= os.time() then
canclick = true
end
if canclick == true then
misc(plr, “Maze Egg”, ‘’)
plr.Data.MazeCD.Value = (os.time() + 86400)
else
print(toHMS(CD))
end
end)
not sure if thats right or not and my brain has stopped working.
I think it’s better if you do it like this
script.Parent.ClickDetector.MouseClick:Connect(function(plr)
local CD = plr.Data.MazeCD.Value + 86400
if CD <= os.time() then
misc(plr, "Maze Egg", "")
plr.Data.MazeCD.Value = (os.time() + 86400)
else
print(toHMS(CD))
end
end)
It’s not really needed to use canclick and you can just use this
ok, it was only just i thought it might be easier lol
So that wouldnt always return flase then as CD is a value set when you first click the button, then if you can click it again it updates it and changes it. (it wont make it 2 days because im adding 86400 twice?)
I’m not sure what you’re trying to say, but this is how the code would work
- Detector is left clicked
- Set CD to the current value in MazeCD.Value added with 86400
- Is CD less than or equal to os.time()?
- If yes, do something and set the MazeCD.Value to os.time() + 86400 (which shouldn’t it be setting it to CD?)
- If no, print CD in HMS format
ive got a cold so my brain is mush atm,
ill do it as bullet points to how it should work…hopefully!
- Detector is clicked
- check if MazeCD.Value + 86400 is less than os.time (its been 24 hours)
- if it has been 24 hours, give pet.
- after giving pet, set MazeCD.Value to os.time + 86400 (set time for another 24 hours)
- if it has not been 24 hours, print MazeCD.Value in HMS format (only as a visual purpose)
What i want to do next once this works is to make a billboardGUI next to the egg so you can see a countdown of how long you have left untill you can claim the egg again, a time untill if you will.
Does that make any more sens?