Any ways to break a for i = 1,10,1 loop on a new function start?

ListFunctions.generateChars = function(Decrease_or_Increase)
ListFunctions.Clean()

if Decrease_or_Increase == "Decrease" then
    CURRENT_INDEX -= 50
else
    CURRENT_INDEX += 50
end

print(CURRENT_INDEX)

local CurPos = StartPos
local GridNum = 15
local Spawned = 0

for i = 50,1,-1 do
    
    local SelectedUserId = List[i + CURRENT_INDEX]
    
    --print(i + CURRENT_INDEX)
    
    if SelectedUserId == nil then print("nil!") continue end
    
    local newCharPos = CharFunctions.CreateCharacter(SelectedUserId,CurPos)
    if newCharPos == nil then continue end
    
    if Spawned >= GridNum then
        Spawned = 0
        CurPos = Vector3.new(StartPos.X,CurPos.Y,CurPos.Z + Offset)
    else
        Spawned += 1
        CurPos = newCharPos + Vector3.new(-Offset,0,0)
    end
    
end

end

This functions launches more than 1 time and while it launches the loop is still working and it’s not stopping, any ways i can stop it on a new function launch?

If I’ve understood your question, you need to use break.
You do something like this (not optimized) :

for i=1, 100 do
  if stopped then
    break;
  end
  -- do your things
  task.wait();
end
-- you've understood

I’m not sure, but can be usefull…

You could use a number before the function is assigned and set it to 0 and see in the for loop if the number goes greater than zero. If so then break the loop and set the number back to zero. Set the number to 1 or just add one to it when the function is first ran.

1 Like

Thanks for helping. Have a good day / night

No problem! You too!! :heart:

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