How to stop loop

local function CleanGrid()
	if currentTask then
		task.cancel(currentTask)
		currentTask = nil
	end
	for _, v in ItemFrames do
		v:Destroy()
	end
	ItemFrames = {}
end

local function UpdateGrid(Items)
	if currentTask then
		warn("cancelling")
		task.cancel(currentTask)
	end
	currentTask = coroutine.create(function()
		for _, ItemData in Items do
			--local ItemButton = CreateItemFrame.Create(ItemData)
			local ItemButton = Instance.new("TextButton")
			ItemButton.Text = ItemData.AssetType
			ItemButton.Parent = Grid
			table.insert(ItemFrames, ItemButton)
			task.wait()
		end
	end)
	task.spawn(currentTask)
end

local function LoadNextPage()
	if not PagesData or PagesData.IsFinished or IsLoadingNextPage then
		return
	end
	IsLoadingNextPage = true
	local success, err = pcall(function()
		PagesData:AdvanceToNextPageAsync()
	end)
	if success then
		UpdateGrid(PagesData:GetCurrentPage())
	end
	IsLoadingNextPage = false
end

function module.LoadSubCategory(SubData)
	CleanGrid()
	Core.CatalogParams.AssetTypes = SubData.AssetTypes or {}
	Core.CatalogParams.BundleTypes = SubData.BundleTypes or {}
	Core.CatalogParams.CreatorName = SubData.CreatorName or ""
	Core.CatalogParams.CategoryFilter = SubData.CategoryFilter or Enum.CatalogCategoryFilter.None
	Core.CatalogParams.SortType = SubData.SortType or Enum.CatalogSortType.Relevance 
	Core.CatalogParams.IncludeOffSale = SubData.IncludeOffSale;
	PagesData = AES:SearchCatalogAsync(Core.CatalogParams)
	UpdateGrid(PagesData:GetCurrentPage())
end

the problem i have:
for example
UpdateGrid() (from module.LoadSubCategory)
while it creating frame launches new UpdateGrid() (from module.LoadSubCategory)

and previous frames from updateGrid are cleaning, but some of frames which didn’t have time to be generated continue to be generated even though I used task.cancel

1 Like
Grid:GetPropertyChangedSignal("CanvasPosition"):Connect(function()
		if IsLoadingNextPage then return end
		local canvasPosition = Grid.CanvasPosition.Y + Grid.CanvasSize.Y.Offset + 250
		local canvasSize = Grid.AbsoluteCanvasSize.Y
		if canvasPosition >= canvasSize then
			LoadNextPage()
		end
	end)

also here’s extend code.

I think you should use coroutine.close() instead of task.cancel() because you are using a coroutine

task.cancel is useless for thread running. You should stop the thread before use task.cancel


local function CleanGrid()
	if currentTask then
		coroutine.close(currentTask)
		currentTask = nil
	end
	for _, v in ItemFrames do
		v:Destroy()
	end
	ItemFrames = {}
end

local coroutines = {}
local function UpdateGrid(Items)
	if currentTask then
		coroutine.close(currentTask)
		currentTask = nil
	end
	
	currentTask = coroutine.create(function()
		--[[
		for _, ItemData in Items do
			--local ItemButton = CreateItemFrame.Create(ItemData)
			local ItemButton = Instance.new("TextButton")
			ItemButton.Text = ItemData.AssetType
			ItemButton.Parent = Grid
			table.insert(ItemFrames, ItemButton)
		end
		]]
		local i = 1
		while Items[i] do
			-- print("generating new frame")
			local ItemData = Items[i]
			local ItemButton = Instance.new("TextButton")
			ItemButton.Text = ItemData.AssetType
			ItemButton.Parent = Grid
			table.insert(ItemFrames, ItemButton)
			i += 1
			task.wait()
		end
	end)
	
	coroutine.resume(currentTask)
end

still not getting desired result

nothing changes, not getting desired result

That’s because you don’t destroy the previous frames after cancelling currentTask.

local function UpdateGrid(Items)
    CleanGrid()
    currentTask = task.spawn(function()
        ...
    end)
end

no, i need this because of the pages and advancetonextpageasync.

well actually fixed this by myself just used promisemodule.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.