I’d honestly stop using tick() entirely. And just set the counter to 86400 when it resets, then you decrement 1 every second. In your text display, just use the value directly as it represents the number of seconds until available.
Save it in a datastore? I guess it depends on your design. Do you want this timer to reset after a player has been in the game for a full day, or after a full day has elapsed? If it’s the latter, you should set the value to be when the feature is available.
local unlocksAt = os.time() + 86400
That way it remains consistent regardless if the user is online or not. At this point you wouldn’t need to connect to IntValue.Changed and instead just have a client-side loop that grabs the local os.time() versus the unlocksAt time().
Nah, because you’re going to actually perform these checks on the server before allowing the player to claim a prize. What their UI says doesn’t affect what your server code does.
local player = game:GetService("Players").LocalPlayer
local LastPrizeClaim = player:WaitForChild("LastPrizeClaim")
local MapLayout = game.Workspace:WaitForChild("MapLayOut")
local TimerLabel = MapLayout:WaitForChild("VIPzone"):WaitForChild("DailyChest"):WaitForChild("Timer"):WaitForChild("BillboardGui"):WaitForChild("TimerText")
while true do
task.wait(1)
if LastPrizeClaim.Value > 0 then
local RemaningTime = 86400 - (os.time() - LastPrizeClaim.Value)
local hours = math.floor(RemaningTime / 3600)
local minutes = (RemaningTime - (hours * 3600)) / 60
local seconds = RemaningTime % 60
print(seconds)
RemaningTime = string.format("%02i:%02i:%02i",
hours,
minutes,
seconds)
print(RemaningTime)
TimerLabel.Text = RemaningTime
elseif LastPrizeClaim.Value <= 0 and TimerLabel.Text ~= "0" then
print("text changed")
TimerLabel.Text = "Daily Reward Available"
end
end
server script in ServerScriptService:
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local OpenSound = game:GetService("ReplicatedStorage"):WaitForChild("SoundEffects"):WaitForChild("EnterZone")
local MoneyPrize = 5000
Players.PlayerAdded:Connect(function(player)
local LastPrizeClaim = Instance.new("NumberValue",player)
LastPrizeClaim.Name = "LastPrizeClaim"
LastPrizeClaim.Value = 0
RunService.Heartbeat:Connect(function()
local character = player.Character or player.CharacterAdded:Wait()
local HumanoidRootPart = character:FindFirstChild("HumanoidRootPart")
local ChestZone = game.Workspace:WaitForChild("MapLayOut"):WaitForChild("VIPzone"):WaitForChild("DailyChest"):WaitForChild("ChestZone")
if HumanoidRootPart then
local distance = (HumanoidRootPart.Position - ChestZone.Position).Magnitude
-- if a day has passed since the last claim, give rewards
if distance < 9 and os.time() - LastPrizeClaim.Value > 86400 then
LastPrizeClaim.Value = os.time()
local Money = player:WaitForChild("leaderstats"):WaitForChild("Money")
OpenSound:Play()
Money.Value += MoneyPrize
end
end
end)
end)