My script won't add one to a NumberValue?

I’m trying to make my script add one to an IntValue so I can create a working day system in my game. For some reason, whenever the ClockTime hits 0, it doesn’t add one like it’s meant to. I’ve tried adding a loop, using a print value, and a few other things, but none have worked.

local lighting = game:GetService("Lighting")
local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage:findFirstChild("Time")

while wait(.9) do
 local hours = math.floor(lighting:GetMinutesAfterMidnight() / 60)
 local suffix = "AM"
 local minutes = lighting:GetMinutesAfterMidnight() - (hours * 60)
 if hours > 11 then
  hours = hours - 12
  suffix = "PM"
 end

 if hours == 0 then
  hours = 12
 end

 if minutes < 10 then
  minutes = "0" .. minutes
 end

 local text = hours .. ":" .. minutes .. " " .. suffix
 event:FireAllClients(text)
 lighting:SetMinutesAfterMidnight(lighting:GetMinutesAfterMidnight() + 1)
end

if game.Lighting.ClockTime == 0 then
    local val = game.Lighting.DotW
    for i = 1,10 do
    val.Value = val.Value + 1
	print(val.Value)
	end
end
1 Like

Why don’t you use a NumberValue instead? It can be more practical if you wish to drastically change time in the future for example from 2AM to 3PM.

Alright, I have made that change, thank you for the tip.

1 Like

I’m not sure how your game works but the problem might be that the ClockTime is a decimal like 0.01 instead of 0 as the for loop executes only if it’s exactly 0.

The ClockTime hits exactly zero.

NumberValues are just IntValues except they store floats instead of integers. Why would using a NumberValue be helpful if OP wants to make a significant time jump? Nothing stops OP from doing IntValue.Value = 15 on a whim.

I believe the problem stems from the fact that the if statement checking for whether ClockTime is 0 or not only runs once, not every time ClockTime hits 0 at any point.

Try:

lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
    if lighting.ClockTime == 0 then
        local val = game.Lighting.DotW
        for i = 1,10 do
            val.Value = val.Value + 1
            print(val.Value)
        end
    end
end)

Pretty sure that is the solution @Khris420.

It still isn’t adding one when the ClockTime hits 0.

Can we see the script that’s updating the time of day?

The entire space above the section that adds one is what changes the time of day.

local lighting = game:GetService("Lighting")
local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage:findFirstChild("Time")

while wait(.9) do
 local hours = math.floor(lighting:GetMinutesAfterMidnight() / 60)
 local suffix = "AM"
 local minutes = lighting:GetMinutesAfterMidnight() - (hours * 60)
 if hours > 11 then
  hours = hours - 12
  suffix = "PM"
 end

 if hours == 0 then
  hours = 12
 end

 if minutes < 10 then
  minutes = "0" .. minutes
 end

 local text = hours .. ":" .. minutes .. " " .. suffix
 event:FireAllClients(text)
 lighting:SetMinutesAfterMidnight(lighting:GetMinutesAfterMidnight() + 1)
end

Nevermind! I figured out part of the issue! I moved it into another script and that fixed it. Excuse my lack of knowledge on scripting.

Thank you to everyone who helped.

You’re going to cause an infinite loop on these two scripts. Look carefully.

hours = 12
hours = hours - 12
hours = 0
if hours == 0
hours = 12

repeat

You can use this function for 12 hour conversion.

local function to12H(hour)
    hour = hour % 24
    return (hour - 1) % 12 + 1
end

Alright, I’ll make sure to incorporate that into my script.