Disconnect previous function running

The problem I face is whenever I call the below function, a previous call of it is still running in the background because of my repeat loops.

So when I call it a first time, it’s fine works perfectly. But the second time I call it, the first call is still running the background, and thus I get 2x the amount of Notifications because it’s still running from my old job. I need it to basically only run once at anytime

So whenever a player changes their job, I fire this function which basically deals with their jobs time and tells them when to go to work etc.

function JobService:Control(player)
	local User = PlayerData[player.UserId]
	if not User then return end
	
	local JobInfo = JobData[User.Job]
	if not JobInfo then return end

	for _, v in pairs(JobInfo.Days) do
		if v == Day.Value then
			repeat wait() until Lighting:GetMinutesAfterMidnight() == JobInfo.StartTime * 60
			
			Notification:FireClient(player, 'Job', User.Job, 'Start')
			
			repeat wait() until Lighting:GetMinutesAfterMidnight() == JobInfo.EndTime * 60
			
			Notification:FireClient(player, 'Job', User.Job, 'End')
			
			if JobInfo.Pay then
				UpdateCash(player, JobInfo.Pay * (JobInfo.EndTime - JobInfo.StartTime))
			end
				
			UpdateExp(player, JobInfo.Exp)
		end
	end
end

Problem is if I change jobs, this function gets fired again, but! the previous function is still running and so in the previous function, the 2 repeat lines are still going, and so I’ll get a notification for my previous job

repeat wait() until Lighting:GetMinutesAfterMidnight() == JobInfo.StartTime * 60
-- and....
repeat wait() until Lighting:GetMinutesAfterMidnight() == JobInfo.EndTime * 60

So how can I disconnect this function everytime it’s called? Cause if I call this function several times, it’ll be running 7 times over.

The typical answer to running only one function at a time when starting another is to give the function an identity that makes it different from another function, so that the current loop always knows which function is supposed to be running.
e.g, You would compare the function’s job to User.Job and see if it is the same every wait, and if it’s not, then cancel the loop:

function JobService:Control(player)
	local User = PlayerData[player.UserId]
	if not User then return end
	local FnJob = User.Job

	local JobInfo = JobData[User.Job]
	if not JobInfo then return end

	for _, v in pairs(JobInfo.Days) do
		if v == Day.Value then
			while 
				Lighting:GetMinutesAfterMidnight() ~= JobInfo.StartTime * 60 and 
				FnJob == User.Job
			do wait() end
			
			if FnJob == User.Job then
				Notification:FireClient(--[[etc]])
			else
				break
			end
			
			while
				Lighting:GetMinutesAfterMidnight ~= JobInfo.EndTime * 60 and
				FnJob == User.Job
			do wait() end

			if FnJob == User.Job then
				Notification:FireClient(--[[etc]])
			else
				break
			end
		end
	end
end

This assumes that User.Job will change whenever the user changes jobs, and that the user can’t apply to the same job.
Another answer to it, if this does not work, is having some kind of simple JobId field that equals a table (or function), since constructing a new one of these will always yield a unique object that will return false when compared with an old object:

function JobService:Control(player)
	--...
	local FnJobId = User.JobId
	--...
end

--whenever you want to change jobs:
User.JobId = {}