Timer not working proparly

why do the seconds go down by 2 instead of 1?

LastPrizeClaim.Changed:Connect(function()
    
    local RemaningTime = 86400 - (tick() - LastPrizeClaim.Value)
    
    local hours = math.floor(RemaningTime / 3600)
    local minutes = (RemaningTime - (hours * 3600)) / 60
    local seconds = RemaningTime % 60
    
    RemaningTime = string.format("%02i:%02i:%02i",
        hours,
        minutes,
        seconds)

    TimerLabel.Text = RemaningTime
end)

while true do
    task.wait(1)
    if LastPrizeClaim.Value > 0 then
        LastPrizeClaim.Value -= 1
    else
        LastPrizeClaim.Value = tick()
    end    
end

Is this all on the server or still split between server and client? Your clocks won’t be precisely synced.

1 Like

so should i change 1 to 2 or change the script?

it just made it reduce 3 every time lol

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.

1 Like

wait, but how do i save it if i dont use tick?

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

local remainingTime = unlocksAt - os.time()

1 Like

wouldnt the player be able to change the time on their pc to claim as mutch as they want?
idc i just wanna know

and isnt os.time() the same as tick()?

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.

They’re very similar but tick() is timezone dependent where os.time() is UTC and globally will always be stable.

fixed and improved script:

local script in StarterPlayerScripts:

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)

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