How to properly stop a loop within a function when the function gets called?

I want to make it so that whenever a different team captures a flag it stops the loop that gives the other team a currency and starts the loop that gives the team that captured it a currency.

When I try to stop the 1st while loop it somehow doubles the 2nd while loop’s speed. But it works fine if the 2nd while loop starts first and then gets stopped to start the 1st while loop. This problem does not occur when ‘‘Empire’’ captures the flag from ‘‘Kingdom’’ (see video)
https://streamable.com/bq4swh

I’ve tried looking at the dev forum for a solution, and i’ve tried using breaks but it did not stop the speeding up.

The code may seem a bit makeshift (I don’t use loops often). Everytime the function is called im setting the 2 booleans to false so both loops ‘‘stop’’ and then It starts the different loop by setting that boolean to true.

I’ve decided to not send the code that sends the parameters since that isnt where the problem is.

Here is the code:

local suncontrolled = false
local kingdomcontrolled = false

function mineflag(number) 
	suncontrolled = false
	kingdomcontrolled = false
	
	if number == 1 then 
			suncontrolled = true
	elseif
		number == 2 then 
			kingdomcontrolled = true	
	end
	
	
	while suncontrolled == true do
		module.SunEmpire.Money = module.SunEmpire.Money + 1
		print("EmpireCurrency", module.SunEmpire.Money)
		wait(1)	
	end
			
		
	while kingdomcontrolled == true do 
		module.Kingdom.Money = module.Kingdom.Money + 1 
		print("KingdomCurrency", module.Kingdom.Money)
		wait(1)		
	end
	
end

I appreciate all help & tips on how to fix this, or suggestions on how to make this work some other way! :happy1:

Instead of two loops, have one that has an if statement in it. If sunControled then give the Sun Empire a reward. Elseif kingdomControlled, give Kingdom a reward. This can be an infinite loop placed in an anonymous spawned function or another script so that you can continue doing other things in your main script like ending the game (if you do that)

You can also add another variable that causes the loop to exit. I recommend making a counter and if it’s value is higher then then it started, stop the loop. This allows starting and stopping the loop quickly to not cause multiple loops to continue ruining.

2 Likes

Heyo, thanks for the response.

I could not fix the problem though, ive removed the second loop and added an if statement in the single loop and whenever the flag gets captured by the kingdom team, the sun team still receives the reward.

I didn’t quite understand the counter part, as in how I would execute it.

local suncontrolled = false
local kingdomcontrolled = false

function mineflag(number)
	 
	local suncontrolled = false
	local kingdomcontrolled = false
	
	if number == 1 then 
		suncontrolled = true
			
	elseif
		number == 2 then 
		kingdomcontrolled = true
	end
	
	while true do	
		if suncontrolled == true then
			module.SunEmpire.Money = module.SunEmpire.Money + 1
			print("EmpireCurrency", module.SunEmpire.Money)
			wait(1)	
			
		elseif
			kingdomcontrolled == true then
			module.Kingdom.Money = module.Kingdom.Money + 1 
			print("KingdomCurrency", module.Kingdom.Money)
			wait(1)
			
		end
	end
end

The problem right now is that the function mineflag never ends. Depending on how you call it you might end up having multiple versions of it running at the same time.

How about you put the while outside of the mineflag function:

local suncontrolled = false
local kingdomcontrolled = false

function mineflag(number)
	 
	if number == 1 then 
		suncontrolled = true
			
	elseif
		number == 2 then 
		kingdomcontrolled = true
	end
	

end

--[[ Put here anything else in the code before starting the while loop ]]

while true do	
	if suncontrolled == true then
		module.SunEmpire.Money = module.SunEmpire.Money + 1
		print("EmpireCurrency", module.SunEmpire.Money)
		wait(1)	
			
	elseif
		kingdomcontrolled == true then
		module.Kingdom.Money = module.Kingdom.Money + 1 
		print("KingdomCurrency", module.Kingdom.Money)
		wait(1)
			
	end
end

Thank you, with you and @IdiomicLanguage’s input I finally got it working! If I could give 2 solutions I would

1 Like