Need help with task.spawn()

I have the following “loop” in my game:

local function startAutoRolling()
	while isAutoRolling and not rollDisabled do
		rollDisabled = true
		roll(mainGui.Assets, rollButton)
	end
end

To start running the function I use:

task.spawn(startAutoRolling)

One of the things it checks for is if rollDisabled is true or not, if it is true it stops running the function, but that means that I need to run task.spawn(startAutoRolling) again to make it run.

Will running task.spawn over and over again lag my game (or the player?) and Is there a different way of doing this besides task.spawn if it does cause lag.

Thanks!

1 Like

Hey! Is there a particular reason why you don’t set rollDisabled to false right after the roll? I’m assuming it serves the purpose of a debounce.

local function startAutoRolling()
	while isAutoRolling and not rollDisabled do
		rollDisabled = true
		roll(mainGui.Assets, rollButton)
        rollDisabled = false -- right here
	end
end

After using task.Spawn() and it stops I see no reason why it should lag the game unless you are overlapping them hundreds of times without the loops ending.

If the loop doesn’t end though you could just use return:

local function startAutoRolling()
	while isAutoRolling and not rollDisabled do

		rollDisabled = true
		roll(mainGui.Assets, rollButton)
		
	end
	return
end

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