Why doesn't this script work?

For some reason the script will not add 1 to it’s value every time the day changes.
Yeah I know the script is ugly but I don’t exactly know how to make it look beautiful

local day = 0
local change = game.Lighting.ClockTime
local debounce = false
local weekday = script.Parent.Text

while true do
	if change == 0 and debounce == false then
		debounce = true
		day += 1
	end

	if change >= 1 and debounce == true then
		debounce = false
	end
	if day == 0 then
		weekday = "Monday"
	elseif day == 1 then
		weekday = "Tuesday"
	elseif day == 2 then
		weekday = "Wednesday"
	elseif day == 3 then
		weekday = "Thursday"
	elseif day == 4 then
		weekday = "Friday"
	elseif day == 5 then
		weekday = "Saturday"
	elseif day ==  6 then
		weekday = "Sunday"
	end

	if day == 8 then
		day = 0
	end
	wait(.1)
end

You haven’t shown the code editing clock time, also depending on how you’ve written that piece of code clock time might never actually be “0”, you would want to use math.floor() as it is possible that it’s a decimal. As for the snippet shown the usage of the if statement is unnecessary. I’ve polished up your code for you.

local day = 1
local debounce = false
local weekday = script.Parent.Text


local Formatting = {
	[1] = "Monday",
	[2] = "Tuesday",
	[3] = "Wednesday",
	[4] = "Thursday",
	[5] = "Friday",
	[6] = "Saturday",
	[7] = "Sunday"
}

game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	local CurrentTime = math.floor(game.Lighting.ClockTime + 0.5) -- +0.5 to make sure its rounded to the nearest whole number 
	if CurrentTime == 0 then 
		if debounce == true then return end 
		debounce = true -- If Your iteration isnt per whole number via ingame hour increments then this is required to make sure its not spamming the days throughout the midnight hour
		if day == 7 then
			day = 1
		else 
			day+=1
			weekday.Text = Formatting[day]
		end
	else 
		debounce = false
	end
end)

2 Likes

I know this is off-topic, but you can just say if debounce then return end, you do not need == true.

You can also shorten this part by using modulus,

day = (day + 1) % 7
weekday.Text = Formatting[day]

Edit : I just realized,

You do this but then you do

That’s not correct, the variable is supposed to be

local weekday = script.Parent

weekday.Text = "Sunday"
2 Likes

I tried this, the solution mostly worked, except for the fact that I get a double Sunday. I fixed up a small mistake pointed out by calvin_coco, and it mostly worked. I did use a print statement to get a better look at what was going on.

Instead of technically making an 8th day, it will basically rename Monday back to Sunday. (As shown in the screenshot)

I’ve tried various ways to fix this by tweaking the code a bit and searching for help via some articles but no luck.
image

If You didn’t use the edit calvin proposed you will want to make the following changes to the snippet i provided: I Accidentally put the modification of the textlabel Text property within the if statement. It Should be outside of it like this:

		if day == 7 then
			day = 1
		else 
			day+=1
		end
                weekday.Text = Formatting[day]
1 Like

Thanks to both you and calvin, It works now.

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