How would I stop this function when the "cB" is activated?

So I have some code, and it has a button know as “CB”.
Anyhow when this button is activated I need it to stop the purchasef Function.
How would I do this?

local function purchasef(item) -- when the CB is pressed, we need to stop this function! 
	-- item folder
	local ItemFolder = game:GetService('ReplicatedStorage'):FindFirstChild('Characters'):FindFirstChild(item)
	
	-- folders item
	local price = ItemFolder.Price
	local owned = ItemFolder.Owned
	
	local purchaseFrame = frame.Purchase
	
	
	if owned.Value == 0 then -- not owned
		local p = false
		
		purchaseFrame.Visible = true
		
		local cB = purchaseFrame.cancel
		local pB = purchaseFrame.purchase
		
		local text = purchaseFrame.Text
		
		text.Text = 'WOULD YOU LIKE TO PURCHASE '..item..' FOR $'..price.Value
		
		pB.Activated:Connect(function()
			if p == false then
				print('Purchasing an item')
				
			end
		end)
		
		cB.Activated:Connect(function() -- this is the CB. I need to stop the function here
			p = true
			
		end)
		
		
		
	else -- owned
		
		
	end
end

If you have a question feel free to ask

local function purchasef(item) -- when the CB is pressed, we need to stop this function! 
	-- item folder
	local ItemFolder = game:GetService('ReplicatedStorage'):FindFirstChild('Characters'):FindFirstChild(item)
	
	-- folders item
	local price = ItemFolder.Price
	local owned = ItemFolder.Owned
	
	local purchaseFrame = frame.Purchase
	
	
	if owned.Value == 0 then -- not owned
		local p = false
		
		purchaseFrame.Visible = true
		
		local cB = purchaseFrame.cancel
		local pB = purchaseFrame.purchase
		
		local text = purchaseFrame.Text
		
		text.Text = 'WOULD YOU LIKE TO PURCHASE '..item..' FOR $'..price.Value
		
		pB.Activated:Connect(function()
			if p == false then
				print('Purchasing an item')
				
			end
		end)
		
		cB.Activated:Connect(function() -- this is the CB. I need to stop the function here
			p = true
            return
		end)
		
		
		
	else -- owned
		
		
	end
end

A return statement terminates the function from running, so you could just do that

It stops the whole function including the purchaseF function?

no it would not stop the function

I’m not 100% certain what your goal is, but if what i’m thinking is right, then what you would do would be something like this:

local function purchasef(item) -- when the CB is pressed, we need to stop this function! 
	-- item folder
	local ItemFolder = game:GetService('ReplicatedStorage'):FindFirstChild('Characters'):FindFirstChild(item)
	
	-- folders item
	local price = ItemFolder.Price
	local owned = ItemFolder.Owned
	
	local purchaseFrame = frame.Purchase
	
	
	if owned.Value == 0 then -- not owned
		local p = false
		
		purchaseFrame.Visible = true
		
		local cB = purchaseFrame.cancel
		local pB = purchaseFrame.purchase
		
		local text = purchaseFrame.Text
		
		text.Text = 'WOULD YOU LIKE TO PURCHASE '..item..' FOR $'..price.Value
		
        local Connection

		Connection = pB.Activated:Connect(function()
			if p == false then
				print('Purchasing an item')
				
			end
		end)
		
		cB.Activated:Connect(function() -- this is the CB. I need to stop the function here
			p = true
            Connection:Disconnect()
			-- Add code to undo/update the frames
		end)
		
		
		
	else -- owned
		
		
	end
end

If this isn’t what you’re looking for can you tell what you are trying to achieve by stopping the function, because there is probably an alternative.

1 Like