How would I make an event happen with a date system, without lagging?

I have a date system that words like so:

I have one text box called “month” and another called “year”

Under “month” is so

while wait(2) do
	script.Parent.Text = tostring(tonumber(script.Parent.Text) + 1)
	if script.Parent.Text == "12" then
		wait(2)
		script.Parent.Text = "1"
	end
end

And under “year” is so

while wait(2) do
	if script.Parent.Parent.month.Text == "12" then
		wait(2)
		script.Parent.Text = tostring(tonumber(script.Parent.Text) + 1)
	end
end

I have a script ontop of all that that has one purpose.
When month reaches a certain value, and when year reaches a certain value, I want to run an event from my module script.

But to constantly check with a while wait(2) do, I feel like that would lag the game.
Is there any better way to do this?

I think it will be fine to just include an if statement within the while loop. If the conditions are not met, there is little effect of an if statement.

Instead of checking the text in your loop, you should just check specifically when the text changes using .Changed, its an event that fires whenever the instance changes.

An example would look something like

script.Parent.Changed:Connect(function()
	if script.Parent.Text == "12" then
		wait(2)
		script.Parent.Text = "1"
	end
end

Thank you! I will be using this.

2 Likes