Make loop include

You don’t make the new index included, for loops are for a fixed number of times that you loop, they are not built to be able to run forever. You could do this with a while loop, which is meant to run whenever a condition is true, because that condition can be changing at an unknown time

I don’t know if its what you want but here’s my code:

local function newFunction(part)
	--code here
end

local connection = script.Parent.ChildAdded:Connect(function(object)
	newFunction(object)
end)

for i, v in pairs(script.Parent:GetChildren()) do
	newFunction(v)
	wait(1)
	
end

connection:Disconnect()



To answer your question, no. It will not be included. It’s impossible to do what you are asking, without incorporating a while loop instead.

You would do something like this:

local Children = folder:GetChildren()

folder.ChildAdded:Connect(function(Child) -- Adding instances
  table.insert(Children, Child)
end)

folder.ChildRemoved:Connect(function(Child) -- Also cover removing instances
  if table.find(Children, Child) then -- Find if there's an index this Child falls under.
    table.remove(Children, table.find(Children, Child)) -- Remove said item from the Children table.
  end
end)

local Current = 1

while true do
  if Current > #Children then
    break
  end
  local Child = Children[Current]
  -- Do something with Child
  task.wait(1) -- wait() is deprecated, use the task library for future projects
  Current += 1
end
1 Like

How’s that so? How is he stuck in the last room?

i dont know how that is possible but it seems hes stuck in a while loop
do you want me to look for errors again?

1 Like

this is just another of my today issues

1 Like

is this going to do what i need it to and sure continue the code after going trough the last child?

1 Like

It will continue the code by breaking the loop after the last child is accounted for. That is the reason for the break statement.

And then it will continue the code normall?
(Do the code nelow the loop?)

2 Likes

Yes. You can code below the loop. It will break after the last child.

Do you have disgord so i can stream what just hwppened after running your code?

1 Like

I do not, no, but anything that unexpected after running the modified version of your code incorporating the while loop would be what would happen as intended by the code.

PLEASE TELL ME I DID IT PROPERLY :sleepy:

local Rush_DB = true
function Rush()
	if Rush_DB then Rush_DB = false

		if script.ENTITY_FOLDER.ACTIVE_RUSH.Value ~= nil then
			script.ENTITY_FOLDER.ACTIVE_RUSH.Value:Destroy()
		else
		end


		script.EVENTS.LIGHTS_CONTROLLER:Fire("FLICKER")
		script.EVENTS.HIDING_HIGHLIGHT:Fire()
		wait(4)

		local Cloned_Rush = script.ENTITY_FOLDER.RUSH.Value:Clone()

		script.ENTITY_FOLDER.ACTIVE_RUSH.Value = Cloned_Rush

		Cloned_Rush.Parent = game.Workspace

		--game.Debris:AddItem(Cloned_Rush,20)


		Cloned_Rush.RUSH.CHASE:Play()
		Cloned_Rush.RUSH.CHASE_INNER:Play()

		local SPAWN_ROOM_NUMBER = script.ROOM_AMOUNT.Value -6

		local SPAWNROOM = script.ACTIVE_ROOMS_FOLDER.Value:FindFirstChild(SPAWN_ROOM_NUMBER)

		Cloned_Rush.CFrame = script.SPAWN_ROOM.Value:FindFirstChild("END").CFrame

		wait(4)



		local Children = script.ACTIVE_ROOMS_FOLDER.Value:GetChildren()

		script.ACTIVE_ROOMS_FOLDER.Value.ChildAdded:Connect(function(Child) -- Adding instances
			table.insert(Children, Child)
		end)

		script.ACTIVE_ROOMS_FOLDER.Value.ChildRemoved:Connect(function(Child) -- Also cover removing instances
			if table.find(Children, Child) then -- Find if there's an index this Child falls under.
				table.remove(Children, table.find(Children, Child)) -- Remove said item from the Children table.
			end
		end)

		local Current = 1

		while true do
			if Current > #Children then
				break
			end
			local Child = Children[Current]
			for a,b in pairs(script.ACTIVE_ROOMS_FOLDER.Value:GetChildren()) do
				if b:FindFirstChild("CHASE_NODES") then
					for c,d in ipairs(b.CHASE_NODES:GetChildren()) do
						if Cloned_Rush.RUSH.DAMAGE.RUNNING.Value == true then
							local anglePart = d
							if anglePart then
								game.TweenService:Create(Cloned_Rush,TweenInfo.new(script.ENEMY_SPEED.Value,Enum.EasingStyle.Linear),{CFrame = anglePart.CFrame * CFrame.new(0,0,0)}):Play()
								task.wait(script.ENEMY_SPEED.Value)
							end
						else
						end
					end
				end
				task.wait(1) -- wait() is deprecated, use the task library for future projects
				Current += 1
			end

			if script.ENTITY_FOLDER.ACTIVE_RUSH.Value ~= nil then
				if script.ENTITY_FOLDER.ACTIVE_RUSH.Value.RUSH.DAMAGE.RUNNING.Value == true then
					script.ENTITY_FOLDER.ACTIVE_RUSH.Value:Destroy()
				end
			end

			Rush_DB = true
		end
	end
end

1 Like

you script is still not werking. somehow getting the solution of this topic is smth to flex with

1 Like

There is a very simple answer to this question: A for loop cannot include it…

I highly advise you use .ChildAdded instead of a for loop

Idk if this script works because I can’t find an efficient way to test it.
The method I used was to create a movement function and repeat it if there is any room to check.

local Rush_DB = true
local TweenService = game:GetService("TweenService")
local Entities = script.ENTITY_FOLDER

function Rush()
	if Rush_DB then 
		Rush_DB = false
		
		-- If the event runs when Rush exists, the current one is destroyed
		if Entities.ACTIVE_RUSH.Value ~= nil then
			Entities.ACTIVE_RUSH.Value:Destroy()
		end
		
		-- Activate the warning that the entity is approaching
		script.EVENTS.LIGHTS_CONTROLLER:Fire("FLICKER")
		script.EVENTS.HIDING_HIGHLIGHT:Fire()
		task.wait(4.0)
		
		-- Make the entity appear
		local newRush = Entities.RUSH.Value:Clone()
		local blacklist = {}
		Entities.ACTIVE_RUSH.Value = newRush
		newRush.Parent = game.Workspace
		
		newRush.RUSH.CHASE:Play()
		newRush.RUSH.CHASE_INNER:Play()
		newRush.CFrame = script.SPAWN_ROOM.Value:FindFirstChild("END").CFrame
		task.wait(4.0)
		
		-- Function that checks if there are rooms in which Rush didn't pass
		local function everyRoom()
			for index, room in pairs(script.ACTIVE_ROOMS_FOLDER.Value:GetChildren()) do
				if blacklist[room] ~= nil then
					return false
				end
			end
			return true
		end
		
		-- Move the entity		
		local function moveRush()
			for index, room in pairs(script.ACTIVE_ROOMS_FOLDER.Value:GetChildren()) do
				if room:FindFirstChild("CHASE_NODES") then
					if blacklist[room] == nil then
						table.insert(blacklist, room)
						
						for a, b in ipairs(room.CHASE_NODES:GetChildren()) do
							if newRush.RUSH.DAMAGE.RUNNING.Value == true then
								local anglePart = b
								
								if anglePart then
									local tween = TweenService:Create(
										newRush,
										TweenInfo.new(
											script.ENEMY_SPEED.Value,
											Enum.EasingStyle.Linear),
										{CFrame = anglePart.CFrame * CFrame.new(0,0,0)}
									)
									tween:Play()
									task.wait(script.ENEMY_SPEED.Value)
								end
							end
						end
					end
				end
			end
		end
		
		-- untouched rooms = movement doesn't end
		repeat task.wait()
			moveRush()
		until everyRoom()
		
		if Entities.ACTIVE_RUSH.Value ~= nil then
			if Entities.ACTIVE_RUSH.Value.RUSH.RUNNING.Value == true then
				Entities.ACTIVE_RUSH.Value:Destroy()
			end
		end
		
		Rush_DB = true
	end
end

Anyway if it doesnt work, I made the code friendlier in case someone wants to help you in the future

thanks for trying i will check if it works asap

nope it doesnt work apparently ): i am getting so tired of this problem