Help with loops not stopping!

I’m making a game that gives a player leaderstats every minute that’s passed, it works, its just I have a gamepass that boosts these speeds. But it requires a button to purchase, and I made a button wheather you can toggle the gamepass on/off using loops. It’s just when i turn it off/on, the loops don’t seem to stop. I need assistance on how I could fix or improve this. Here is the code in a localScript inside my gui.

local marketplaceservice = game:GetService("MarketplaceService")

local player = game.Players.LocalPlayer
local plrHidden = player:WaitForChild("hiddenstats")
local plrStats = player:WaitForChild("leaderstats")
local boosPassId = 668758510 --replace with your gamepass id


local boostButton = script.Parent:WaitForChild("CanvasGroup"):WaitForChild("Buttons"):WaitForChild("ToggleBoost"):WaitForChild("ImageButton") -- frame
local boostButton2 = script.Parent:WaitForChild("CanvasGroup"):WaitForChild("Buttons"):WaitForChild("ToggleBoost"):WaitForChild("ImageButton2") -- frame



local timeText = script.Parent.CanvasGroup.LeaderstatsFrame.TimeFrame:WaitForChild("TimeText")

local Cash = plrStats:WaitForChild("Cash")
local seconds = plrHidden:WaitForChild("Seconds")
local minutes = plrHidden:WaitForChild("Minutes")

local RS = game:GetService("ReplicatedStorage")
local rent = RS.RemoteEvents:WaitForChild("RentAlert")
local rentGiver = RS.RemoteEvents:WaitForChild("RentGiver")


rentGiver.OnClientEvent:Connect(function(money)
	Cash.Value += money
	print("given "..money.." to "..player.Name)
end)

boostButton.MouseButton1Click:Connect(function()
	if marketplaceservice:UserOwnsGamePassAsync(player.UserId,boosPassId) then
		print("player own gamepass")
		if boostButton.Visible == true then
			boostButton2.Visible = true
			boostButton.Visible = false
			while true do 
				wait(0.2)
				timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
				seconds.Value += 1
				--print("Second + 1")
				if seconds.Value == 60 then
					--print("Rent")
					rent:FireServer(player)
					seconds.Value = 0
					minutes.Value += 1
				end
				if seconds.Value <= 9 and minutes.Value <=9 then
					timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
					--print("s & m less than 9")
				elseif seconds.Value >= 9 and minutes.Value >=9 then
					timeText.Text = minutes.Value..":"..seconds.Value
					--print("s & m more than 9")
				elseif seconds.Value >= 9 and minutes.Value <=9 then
					timeText.Text = "0"..minutes.Value..":"..seconds.Value
					--print("s more than 9 m less than 9")
				elseif seconds.Value <= 9 and minutes.Value >=9 then
					timeText.Text = minutes.Value..":".."0"..seconds.Value
					--print("m more than 9 s less than 9")
				end
				if minutes.Value == 24 then
					minutes.Value = 0
					seconds.Value = 0
					--print("ResetTime")
				end
			end
	else
		marketplaceservice:PromptGamePassPurchase(player,boosPassId)
		print("player doesnt own gamepass")
	end
	end
	end)

boostButton2.MouseButton1Click:Connect(function()
		if boostButton2.Visible == true then
			boostButton.Visible = true
			boostButton2.Visible = false
			while true do 
				wait(0.5)
				timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
				seconds.Value += 1
				--print("Second + 1")
				if seconds.Value == 60 then
					--print("Rent")
					rent:FireServer(player)
					seconds.Value = 0
					minutes.Value += 1
				end
				if seconds.Value <= 9 and minutes.Value <=9 then
					timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
					--print("s & m less than 9")
				elseif seconds.Value >= 9 and minutes.Value >=9 then
					timeText.Text = minutes.Value..":"..seconds.Value
					--print("s & m more than 9")
				elseif seconds.Value >= 9 and minutes.Value <=9 then
					timeText.Text = "0"..minutes.Value..":"..seconds.Value
					--print("s more than 9 m less than 9")
				elseif seconds.Value <= 9 and minutes.Value >=9 then
					timeText.Text = minutes.Value..":".."0"..seconds.Value
					--print("m more than 9 s less than 9")
				end
				if minutes.Value == 24 then
					minutes.Value = 0
					seconds.Value = 0
					--print("ResetTime")
					end
				end
			end
		end)


if boostButton.Visible == true then
	while true do 
		wait(0.5)
		timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
		seconds.Value += 1
		--print("Second + 1")
		if seconds.Value == 60 then
			--print("Rent")
			rent:FireServer(player)
			seconds.Value = 0
			minutes.Value += 1
		end
		if seconds.Value <= 9 and minutes.Value <=9 then
			timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
			--print("s & m less than 9")
		elseif seconds.Value >= 9 and minutes.Value >=9 then
			timeText.Text = minutes.Value..":"..seconds.Value
			--print("s & m more than 9")
		elseif seconds.Value >= 9 and minutes.Value <=9 then
			timeText.Text = "0"..minutes.Value..":"..seconds.Value
			--print("s more than 9 m less than 9")
		elseif seconds.Value <= 9 and minutes.Value >=9 then
			timeText.Text = minutes.Value..":".."0"..seconds.Value
			--print("m more than 9 s less than 9")
		end
		if minutes.Value == 24 then
			minutes.Value = 0
			seconds.Value = 0
			--print("ResetTime")
		end
	end
end
2 Likes

is boostButton2 the off button correct?

1 Like

BoostButton2 is the on button, its appears once BoostButton1 is clicked and can only turn on if you have the gamepass, it changes the speed to 0.2 rather than 0.5 as can be seen, its just the loops stack, and don’t turn off once i click the button, which I need assistance with.

1 Like

so you want the non-booster loop to stop once the booster is on? and also add a way to stop stacking loops? try adding a one time only bool. So if the booster is turned on it will check if the one time only variable is false to run. And when it runs the booster, make it set the one time bool to true

1 Like

Yes, i want the non booster loop to stop once i turn the booster loop on and vice versa and stop stacking them. How could i implement this one time bool

1 Like

Make a backup of your script and try this one:

local marketplaceservice = game:GetService("MarketplaceService")

local player = game.Players.LocalPlayer
local plrHidden = player:WaitForChild("hiddenstats")
local plrStats = player:WaitForChild("leaderstats")
local boosPassId = 668758510 --replace with your gamepass id


local boostButton = script.Parent:WaitForChild("CanvasGroup"):WaitForChild("Buttons"):WaitForChild("ToggleBoost"):WaitForChild("ImageButton") -- frame
local boostButton2 = script.Parent:WaitForChild("CanvasGroup"):WaitForChild("Buttons"):WaitForChild("ToggleBoost"):WaitForChild("ImageButton2") -- frame



local timeText = script.Parent.CanvasGroup.LeaderstatsFrame.TimeFrame:WaitForChild("TimeText")

local Cash = plrStats:WaitForChild("Cash")
local seconds = plrHidden:WaitForChild("Seconds")
local minutes = plrHidden:WaitForChild("Minutes")

local RS = game:GetService("ReplicatedStorage")
local rent = RS.RemoteEvents:WaitForChild("RentAlert")
local rentGiver = RS.RemoteEvents:WaitForChild("RentGiver")


rentGiver.OnClientEvent:Connect(function(money)
	Cash.Value = Cash.Value+ money
	print("given "..money.." to "..player.Name)
end)

local OneTimeOnlyBool = false
local OneTimeOnlyBool2 = false
local BoostingEffect = false

boostButton.MouseButton1Click:Connect(function()
	if OneTimeOnlyBool2 == false and marketplaceservice:UserOwnsGamePassAsync(player.UserId,boosPassId) then
		OneTimeOnlyBool2 = true
		BoostingEffect = true
		print("player own gamepass")
		if boostButton.Visible == true then
			boostButton2.Visible = true
			boostButton.Visible = false
			while BoostingEffect == true do 
				wait(0.2)
				timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
				seconds.Value = seconds.Value+1
				--print("Second + 1")
				if seconds.Value == 60 then
					--print("Rent")
					rent:FireServer(player)
					seconds.Value = 0
					minutes.Value = minutes.Value+1
				end
				if seconds.Value <= 9 and minutes.Value <=9 then
					timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
					--print("s & m less than 9")
				elseif seconds.Value >= 9 and minutes.Value >=9 then
					timeText.Text = minutes.Value..":"..seconds.Value
					--print("s & m more than 9")
				elseif seconds.Value >= 9 and minutes.Value <=9 then
					timeText.Text = "0"..minutes.Value..":"..seconds.Value
					--print("s more than 9 m less than 9")
				elseif seconds.Value <= 9 and minutes.Value >=9 then
					timeText.Text = minutes.Value..":".."0"..seconds.Value
					--print("m more than 9 s less than 9")
				end
				if minutes.Value == 24 then
					minutes.Value = 0
					seconds.Value = 0
					--print("ResetTime")
				end
			end
	else
		marketplaceservice:PromptGamePassPurchase(player,boosPassId)
		print("player doesnt own gamepass")
	end
	end
end)

boostButton2.MouseButton1Click:Connect(function()
	if OneTimeOnlyBool == false and boostButton2.Visible == true then
	    OneTimeOnlyBool = true
		boostButton.Visible = true
		boostButton2.Visible = false
		while BoostingEffect == false do 
			wait(0.5)
			timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
			seconds.Value = seconds.Value+1
			--print("Second + 1")
			if seconds.Value == 60 then
				--print("Rent")
				rent:FireServer(player)
				seconds.Value = 0
				minutes.Value = minutes.Value+1
			end
			if seconds.Value <= 9 and minutes.Value <=9 then
				timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
				--print("s & m less than 9")
			elseif seconds.Value >= 9 and minutes.Value >=9 then
				timeText.Text = minutes.Value..":"..seconds.Value
				--print("s & m more than 9")
			elseif seconds.Value >= 9 and minutes.Value <=9 then
				timeText.Text = "0"..minutes.Value..":"..seconds.Value
				--print("s more than 9 m less than 9")
			elseif seconds.Value <= 9 and minutes.Value >=9 then
				timeText.Text = minutes.Value..":".."0"..seconds.Value
				--print("m more than 9 s less than 9")
			end
			if minutes.Value == 24 then
				minutes.Value = 0
				seconds.Value = 0
				--print("ResetTime")
			end
		end
	end
end)


if boostButton.Visible == true then
	while BoostingEffect == false do 
	    OneTimeOnlyBool = true
		wait(0.5)
		timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
		seconds.Value = seconds.Value+1
		--print("Second + 1")
		if seconds.Value == 60 then
			--print("Rent")
			rent:FireServer(player)
			seconds.Value = 0
			minutes.Value = minutes.Value+1
		end
		if seconds.Value <= 9 and minutes.Value <=9 then
			timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
			--print("s & m less than 9")
		elseif seconds.Value >= 9 and minutes.Value >=9 then
			timeText.Text = minutes.Value..":"..seconds.Value
			--print("s & m more than 9")
		elseif seconds.Value >= 9 and minutes.Value <=9 then
			timeText.Text = "0"..minutes.Value..":"..seconds.Value
			--print("s more than 9 m less than 9")
		elseif seconds.Value <= 9 and minutes.Value >=9 then
			timeText.Text = minutes.Value..":".."0"..seconds.Value
			--print("m more than 9 s less than 9")
		end
		if minutes.Value == 24 then
			minutes.Value = 0
			seconds.Value = 0
			--print("ResetTime")
		end
	end
end

does this works?

1 Like

It turns on, but it doesn’t turn off. It’s just stuck on the turned on mode

1 Like

Hmm maybe try this one… (Try learning bool values too or workarounds)

local marketplaceservice = game:GetService("MarketplaceService")

local player = game.Players.LocalPlayer
local plrHidden = player:WaitForChild("hiddenstats")
local plrStats = player:WaitForChild("leaderstats")
local boosPassId = 668758510 --replace with your gamepass id


local boostButton = script.Parent:WaitForChild("CanvasGroup"):WaitForChild("Buttons"):WaitForChild("ToggleBoost"):WaitForChild("ImageButton") -- frame
local boostButton2 = script.Parent:WaitForChild("CanvasGroup"):WaitForChild("Buttons"):WaitForChild("ToggleBoost"):WaitForChild("ImageButton2") -- frame



local timeText = script.Parent.CanvasGroup.LeaderstatsFrame.TimeFrame:WaitForChild("TimeText")

local Cash = plrStats:WaitForChild("Cash")
local seconds = plrHidden:WaitForChild("Seconds")
local minutes = plrHidden:WaitForChild("Minutes")

local RS = game:GetService("ReplicatedStorage")
local rent = RS.RemoteEvents:WaitForChild("RentAlert")
local rentGiver = RS.RemoteEvents:WaitForChild("RentGiver")


rentGiver.OnClientEvent:Connect(function(money)
	Cash.Value = Cash.Value+ money
	print("given "..money.." to "..player.Name)
end)

local OneTimeOnlyBool = false
local OneTimeOnlyBool2 = false
local BoostingEffect = false

boostButton.MouseButton1Click:Connect(function()
    if OneTimeOnlyBool2 == true then
        print("booster off, returning to normal")
        OneTimeOnlyBool2 = false
		BoostingEffect = false
		coroutine.resume(coroutine.create(function()
		    while BoostingEffect == false do 
    			wait(0.5)
    			timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
    			seconds.Value = seconds.Value+1
    			--print("Second + 1")
    			if seconds.Value == 60 then
    				--print("Rent")
    				rent:FireServer(player)
    				seconds.Value = 0
    				minutes.Value = minutes.Value+1
    			end
    			if seconds.Value <= 9 and minutes.Value <=9 then
    				timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
    				--print("s & m less than 9")
    			elseif seconds.Value >= 9 and minutes.Value >=9 then
    				timeText.Text = minutes.Value..":"..seconds.Value
    				--print("s & m more than 9")
    			elseif seconds.Value >= 9 and minutes.Value <=9 then
    				timeText.Text = "0"..minutes.Value..":"..seconds.Value
    				--print("s more than 9 m less than 9")
    			elseif seconds.Value <= 9 and minutes.Value >=9 then
    				timeText.Text = minutes.Value..":".."0"..seconds.Value
    				--print("m more than 9 s less than 9")
    			end
    			if minutes.Value == 24 then
    				minutes.Value = 0
    				seconds.Value = 0
    				--print("ResetTime")
    			end
    		end
		end))
		return
    end
	if OneTimeOnlyBool2 == false and marketplaceservice:UserOwnsGamePassAsync(player.UserId,boosPassId) then
		OneTimeOnlyBool2 = true
		BoostingEffect = true
		print("booster on")
		print("player own gamepass")
		if boostButton.Visible == true then
			boostButton2.Visible = true
			boostButton.Visible = false
			while BoostingEffect == true do 
				wait(0.2)
				timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
				seconds.Value = seconds.Value+1
				--print("Second + 1")
				if seconds.Value == 60 then
					--print("Rent")
					rent:FireServer(player)
					seconds.Value = 0
					minutes.Value = minutes.Value+1
				end
				if seconds.Value <= 9 and minutes.Value <=9 then
					timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
					--print("s & m less than 9")
				elseif seconds.Value >= 9 and minutes.Value >=9 then
					timeText.Text = minutes.Value..":"..seconds.Value
					--print("s & m more than 9")
				elseif seconds.Value >= 9 and minutes.Value <=9 then
					timeText.Text = "0"..minutes.Value..":"..seconds.Value
					--print("s more than 9 m less than 9")
				elseif seconds.Value <= 9 and minutes.Value >=9 then
					timeText.Text = minutes.Value..":".."0"..seconds.Value
					--print("m more than 9 s less than 9")
				end
				if minutes.Value == 24 then
					minutes.Value = 0
					seconds.Value = 0
					--print("ResetTime")
				end
			end
	else
		marketplaceservice:PromptGamePassPurchase(player,boosPassId)
		print("player doesnt own gamepass")
	end
	end
end)

boostButton2.MouseButton1Click:Connect(function()
	if OneTimeOnlyBool == false and boostButton2.Visible == true then
	    OneTimeOnlyBool = true
		boostButton.Visible = true
		boostButton2.Visible = false
		while BoostingEffect == false do 
			wait(0.5)
			timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
			seconds.Value = seconds.Value+1
			--print("Second + 1")
			if seconds.Value == 60 then
				--print("Rent")
				rent:FireServer(player)
				seconds.Value = 0
				minutes.Value = minutes.Value+1
			end
			if seconds.Value <= 9 and minutes.Value <=9 then
				timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
				--print("s & m less than 9")
			elseif seconds.Value >= 9 and minutes.Value >=9 then
				timeText.Text = minutes.Value..":"..seconds.Value
				--print("s & m more than 9")
			elseif seconds.Value >= 9 and minutes.Value <=9 then
				timeText.Text = "0"..minutes.Value..":"..seconds.Value
				--print("s more than 9 m less than 9")
			elseif seconds.Value <= 9 and minutes.Value >=9 then
				timeText.Text = minutes.Value..":".."0"..seconds.Value
				--print("m more than 9 s less than 9")
			end
			if minutes.Value == 24 then
				minutes.Value = 0
				seconds.Value = 0
				--print("ResetTime")
			end
		end
	end
end)


if boostButton.Visible == true then
	while BoostingEffect == false do 
	    OneTimeOnlyBool = true
		wait(0.5)
		timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
		seconds.Value = seconds.Value+1
		--print("Second + 1")
		if seconds.Value == 60 then
			--print("Rent")
			rent:FireServer(player)
			seconds.Value = 0
			minutes.Value = minutes.Value+1
		end
		if seconds.Value <= 9 and minutes.Value <=9 then
			timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
			--print("s & m less than 9")
		elseif seconds.Value >= 9 and minutes.Value >=9 then
			timeText.Text = minutes.Value..":"..seconds.Value
			--print("s & m more than 9")
		elseif seconds.Value >= 9 and minutes.Value <=9 then
			timeText.Text = "0"..minutes.Value..":"..seconds.Value
			--print("s more than 9 m less than 9")
		elseif seconds.Value <= 9 and minutes.Value >=9 then
			timeText.Text = minutes.Value..":".."0"..seconds.Value
			--print("m more than 9 s less than 9")
		end
		if minutes.Value == 24 then
			minutes.Value = 0
			seconds.Value = 0
			--print("ResetTime")
		end
	end
end

Hope this new script helps!

1 Like

Make sure to set BoostingEffect To false Cause I only see one line of BoostingEffect And that is to turn on. But I don’t see where you are setting it to false

The code above me seems to set the value to false

2 Likes

The scripts still just turns it on, although the loops dont stack which is progress. This video shows what happens if that helps!
Imgur Video
Video

1 Like

Oh, this is because roblox Lua focuses on a single loop and if the loop will continue to run, it wont run codes beneath it. try using coroutine for loops

1 Like

how would i implement that here?

1 Like

example:

coroutine.resume(coroutine.create(function()
         while true do
               task.wait()
               print("im running")
         end
end))

print("I didnt get stopped by the loop")

Edit: did using coroutine worked for you?

1 Like

how would i put it into my code though, I’m new to coroutines, thanks!

1 Like

like this:

coroutine.resume(coroutine.create(function()
         --your loop here
end))

sorry if im inactive sometimes!

1 Like

For example, like this?

if boostButton.Visible == true then
	while BoostingEffect == false do 
		OneTimeOnlyBool = true
		coroutine.resume(coroutine.create(function()
			wait(0.5)
			timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
			seconds.Value = seconds.Value+1
			--print("Second + 1")
			if seconds.Value == 60 then
				--print("Rent")
				rent:FireServer(player)
				seconds.Value = 0
				minutes.Value = minutes.Value+1
			end
			if seconds.Value <= 9 and minutes.Value <=9 then
				timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
				--print("s & m less than 9")
			elseif seconds.Value >= 9 and minutes.Value >=9 then
				timeText.Text = minutes.Value..":"..seconds.Value
				--print("s & m more than 9")
			elseif seconds.Value >= 9 and minutes.Value <=9 then
				timeText.Text = "0"..minutes.Value..":"..seconds.Value
				--print("s more than 9 m less than 9")
			elseif seconds.Value <= 9 and minutes.Value >=9 then
				timeText.Text = minutes.Value..":".."0"..seconds.Value
				--print("m more than 9 s less than 9")
			end
			if minutes.Value == 24 then
				minutes.Value = 0
				seconds.Value = 0
				--print("ResetTime")
			end		
	end))
	end
end
1 Like

yeah just like that. Try to implement them to your loops so it doesnt pause the script.
Edit: oh wait theres something wrong
Edit 2: you misplaced the coroutine… here:

if boostButton.Visible == true then
coroutine.resume(coroutine.create(function()
	while BoostingEffect == false do 
		OneTimeOnlyBool = true
			wait(0.5)
			timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
			seconds.Value = seconds.Value+1
			--print("Second + 1")
			if seconds.Value == 60 then
				--print("Rent")
				rent:FireServer(player)
				seconds.Value = 0
				minutes.Value = minutes.Value+1
			end
			if seconds.Value <= 9 and minutes.Value <=9 then
				timeText.Text = "0"..minutes.Value..":".."0"..seconds.Value
				--print("s & m less than 9")
			elseif seconds.Value >= 9 and minutes.Value >=9 then
				timeText.Text = minutes.Value..":"..seconds.Value
				--print("s & m more than 9")
			elseif seconds.Value >= 9 and minutes.Value <=9 then
				timeText.Text = "0"..minutes.Value..":"..seconds.Value
				--print("s more than 9 m less than 9")
			elseif seconds.Value <= 9 and minutes.Value >=9 then
				timeText.Text = minutes.Value..":".."0"..seconds.Value
				--print("m more than 9 s less than 9")
			end
			if minutes.Value == 24 then
				minutes.Value = 0
				seconds.Value = 0
				--print("ResetTime")
			end		
	end
end))
end
1 Like

do i do that to all the scripts?

1 Like

thats optional :sweat_smile: but its a good practice to use it only when neccesarily. Search up coroutine for more details, that may help you

1 Like

You can use break to end a while loop

Hope that helps

1 Like