# Implementation Help

**URL:** <https://forum.defold.com/t/implementation-help/79848>\
**Category:** Questions\
**Created:** [February 21, 2025, 7:33pm UTC](https://forum.defold.com/t/implementation-help/79848 "2025-02-21T19:33:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DJIGNITE](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/djignite/32/38837_2.png) [@DJIGNITE](https://forum.defold.com/u/DJIGNITE)\
**Post date:** [February 21, 2025, 7:33pm UTC](https://forum.defold.com/t/implementation-help/79848/1 "2025-02-21T19:33:38Z")

</div>

I for the life of me cannot figure out why when switching months my buttons do not clone to the next month. Any help would be appreciated.

This script is over 500 lines, so ill include the relevant variables and functions here.  
If you would like to see the full script:[Click Here](https://github.com/DJIGNITE/ui_calendar/blob/main/ui_days.gui_script)

Variables:

```lua
local DATEYEAR = os.date("%Y")
local DATEMONTH = os.date("%m")
local CALYEAR = 2025
local CALMONTH = tonumber(os.date("%m"))
local CALENDARSTATE = {}
local APPOINTMENTS = {}
local cloned_buttons = {}
local cloned_texts = {}

local MERGED_APPOINTMENTS = {} -- Table to store split/merged appointments

local SAVEDAPPOINTMENTS = {}

```

changemonth():

```lua
local function changemonth(direction)
	save_calendar_state()
	
	-- Change month
	CALMONTH = CALMONTH + direction
	if CALMONTH < 1 then
		CALMONTH = 12
		CALYEAR = CALYEAR - 1
	elseif CALMONTH > 12 then
		CALMONTH = 1
		CALYEAR = CALYEAR + 1
	end

	local num_days = get_num_days(CALYEAR, CALMONTH)
	local start_time = os.time({year = CALYEAR, month = CALMONTH, day = 1})
	local start_wday = tonumber(os.date("%w", start_time))

	updatecalendardays(num_days, start_wday)
	clone_day_buttons() 
	load_calendar_state()
end

```

clone\_day\_buttons():

```lua
local function clone_day_buttons()
	local num_days = get_num_days(CALYEAR, CALMONTH)
	local start_time = os.time({year = CALYEAR, month = CALMONTH, day = 1})
	local start_wday = tonumber(os.date("%w", start_time))

	-- Clear existing buttons
	for _, button in pairs(cloned_buttons) do
		gui.delete_node(button)
	end
	cloned_buttons = {} -- Reset to an empty table

	for i = start_wday, num_days + start_wday do

		local day_container = gui.get_node("ui_day" .. i)
		if day_container then
			local cloned_button = gui.clone(gui.get_node("button_day"))

			-- Set unique ID
			local new_id = i
			gui.set_id(cloned_button, new_id)

			-- Set text to match the corresponding day
			gui.set_text(cloned_button, tostring(i))

			-- Parent button to the correct day container
			gui.set_parent(cloned_button, day_container, false)

			-- Position the button inside the container
			gui.set_position(cloned_button, vmath.vector3(61, 25, 0))

			-- Store in table with specific ID
			cloned_buttons[new_id] = cloned_button
			-- pprint(cloned_buttons)

		else
			print("Warning: Missing container for day " .. i)
		end
	end
	gui.set_enabled(gui.get_node("button_day"), "disable")
	gui.set_visible(gui.get_node("button_day"), false)
	-- pprint(cloned_buttons)
end

```

Thank you in advance. Also, if you look at the full script any tips you have would be well received. This is my first defold project and i’m learning lua as i go along.

---

<div class="post-metadata">

**Author:** ![NaakkaDev](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/naakkadev/32/34927_2.png) [@NaakkaDev](https://forum.defold.com/u/NaakkaDev)\
**Post date:** [February 22, 2025, 6:08am UTC](https://forum.defold.com/t/implementation-help/79848/2 "2025-02-22T06:08:02Z")

</div>

After a quick look I noticed this: `gui.set_enabled(gui.get_node("button_day"), "disable")`. It should be `false` instead of `"disable"`. My eyes didn’t catch anything else wrong and it sounds strange that it works on the init but not when changing the month, if I understood correctly.

---

<div class="post-metadata">

**Author:** ![DJIGNITE](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/djignite/32/38837_2.png) [@DJIGNITE](https://forum.defold.com/u/DJIGNITE)\
**Post date:** [February 22, 2025, 2:18pm UTC](https://forum.defold.com/t/implementation-help/79848/3 "2025-02-22T14:18:52Z")

</div>

Omg, thank you. I know what the issue is after you pointed out that the button is disabled. I am cloning a disabled button on changemonth. I need to re enable button\_day at the beginning of the clone\_day\_buttons function then clone the buttons.

So the updated function should look like this

```lua
local function clone_day_buttons()
	local num_days = get_num_days(CALYEAR, CALMONTH)
	local start_time = os.time({year = CALYEAR, month = CALMONTH, day = 1})
	local start_wday = tonumber(os.date("%w", start_time))
gui.set_enabled(gui.get_node("button_day"), true)
gui.set_visible(gui.get_node("button_day"), true)

	-- Clear existing buttons
	for _, button in pairs(cloned_buttons) do
		gui.delete_node(button)
	end
	cloned_buttons = {} -- Reset to an empty table

	for i = start_wday, num_days + start_wday do

		local day_container = gui.get_node("ui_day" .. i)
		if day_container then
			local cloned_button = gui.clone(gui.get_node("button_day"))

			-- Set unique ID
			local new_id = i
			gui.set_id(cloned_button, new_id)

			-- Set text to match the corresponding day
			gui.set_text(cloned_button, tostring(i))

			-- Parent button to the correct day container
			gui.set_parent(cloned_button, day_container, false)

			-- Position the button inside the container
			gui.set_position(cloned_button, vmath.vector3(61, 25, 0))

			-- Store in table with specific ID
			cloned_buttons[new_id] = cloned_button
			-- pprint(cloned_buttons)

		else
			print("Warning: Missing container for day " .. i)
		end
	end
	gui.set_enabled(gui.get_node("button_day"), false)
	gui.set_visible(gui.get_node("button_day"), false)
	-- pprint(cloned_buttons)
end

```

---

<div class="post-metadata">

**Author:** ![NaakkaDev](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/naakkadev/32/34927_2.png) [@NaakkaDev](https://forum.defold.com/u/NaakkaDev)\
**Post date:** [February 22, 2025, 2:31pm UTC](https://forum.defold.com/t/implementation-help/79848/4 "2025-02-22T14:31:06Z")

</div>

Aah, yes of course! I have done that once or twice myself now that you said it. 😄

---

<div class="post-metadata">

**Author:** ![DJIGNITE](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/djignite/32/38837_2.png) [@DJIGNITE](https://forum.defold.com/u/DJIGNITE)\
**Post date:** [February 22, 2025, 2:46pm UTC](https://forum.defold.com/t/implementation-help/79848/5 "2025-02-22T14:46:07Z")

</div>

I spent 4 hours yesterday trying to tweak and figure it out and i did it in 30 seconds after i woke up this morning with your help.
