Extending for loop efficiently?

Simple question that i cant find an answer for. Thanks for any help!

		local goal = burst -- burst = 6 in this instance
		
		for i =1,goal,1 do			
			nearestTarget = nil

			nearestTarget= module.GetTarget(range, script.Parent.HumanoidRootPart,ghost)
			
			if(nearestTarget ~= nil) then
				spawn(function()
					
					if left == true then
						module.Animation(fireanim,script.Parent:WaitForChild("Humanoid"),0,1)
						module.Flash(flare,"a",0.1)
						left = false
					else
						module.Animation(fireanim2,script.Parent:WaitForChild("Humanoid"),0,1)
						module.Flash(flare2,"a",0.1)
						left = true
					end
			
					Fire()
				end)
			else
				goal = goal +1 -- this doesnt work as well!
				-- i = i - 1 also doesnt work!

			end
			
			wait(fr)
		end
		
		spin:Play()
		module.Animation(script.Parent["Spinny :)"],script.Parent:WaitForChild("Humanoid"),0,1)

You could use a while loop instead if you want to change i in the middle of the loop. Just set the initial condition as while i <= goal do and put i += 1 before the end statement, and I believe both of the options would work perfectly fine

1 Like

Thanks, also i didnt know you could use += or -= in Lua, lol

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