Is there a way to redo a for loop?

Is there a way to make a for loop activate forever? If I do this:

local something = workspace.Baseplate:GetChildren()

while wait(1) do
	for i,v in pairs(something) do
		print(v.Name)
	end
end

and I add another instance to the baseplate, it won’t print it. I need to figure out a way to do this so I can check if a part has a specific value. If I move a part from replicated storage to workspace, it won’t count that object even though it has the specific value. Is there anyway to do this?

1 Like
while wait(1) do
    local something = workspace.Baseplate:GetChildren()
	for i,v in pairs(something) do
		print(v.Name)
	end
end
1 Like

I just figured that out on my own. Well this is awkward.

2 Likes

Put your definition of the something variable inside your while loop. It should get garbage collected at the end of the loop.

while wait(1) do
    local something = workspace.Baseplate:GetChildren()
	for i,v in pairs(something) do
		print(v.Name)
	end
end
2 Likes

This isn’t fixing my problem for some reason, I copied your script after modifying my own after that didn’t work, and it still didn’t work. Any reason why?

Did the script return any errors? If so, could you show them to me?

1 Like

No errors, It just said “Decal(x7)” when I added a texture.

1 Like

Interesting. Could you grab me a short snippet of the code?

Alright, it looks terrible and messy but somehow works:

while wait(1) do
	local workspaceT = workspace:GetChildren()
	local TS = game:GetService("TweenService")
		for i,tile in pairs(workspaceT) do

there are more table, but they are under the same while loop, so I don’t think that could be the issue. If you want, I could share the whole code.

I don’t need you to share all of the code, but could you share some of the code that doesn’t produce the effect you want?

1 Like

All of it works, the only time it doesn’t is when I add another part with an Object Value named “TileD” (If a part has an object value called TileD, then if you step on it it will dissapear, or that is what is supposed to happen normally)

1 Like

If you care about descendants rather than direct children, you need GetDescendants instead of GetChildren.

1 Like

Garbage collector will not necessarily run at the end of the loop. The frequency and exact moment of garbage collection is slightly more complex. The only thing that for sure happens at the end of the loop is that the reference to the table something is removed, so nothing references that table any more. And when the garbage collector runs it will deallocate the memory.

2 Likes

I don’t understand why I’d use GetDescentants instead of GetChildren, the object value is the child of the part.

2 Likes

Are you able to provide more code? I could probably reconstruct the code, but it would be easier to solve your problem if you provide your own code.

1 Like

Here is the terrible code. It’s in ServerScriptService and is (obviously) a server script.

while wait(1) do
	local workspaceT = workspace:GetChildren()
	local TS = game:GetService("TweenService")
		for i,tile in pairs(workspaceT) do
			if tile:IsA("Part") then
				local tileT = tile:GetChildren()
				for i,tileTD in pairs(tileT) do
					if tileTD:IsA("ObjectValue") then
						if tileTD.Name == "TileD" then
							tileTD.Parent.Touched:Connect(function()
								local info = TweenInfo.new(0.6,
									Enum.EasingStyle.Sine,
									Enum.EasingDirection.In,
									0,
									false,
									0
								)
								
								local tProp = {
									Size = Vector3.new(6,0.01,6),
									Transparency = 1,
								}
								
								local tileTween = TS:Create(tileTD.Parent,info,tProp)
								tileTween:Play()
								tileTD.Parent.Material = Enum.Material.Neon
								wait(0.6)
								tileTD.Parent.CanCollide = false
							end)
						end
					end
				end
			end
		end
end

Most of it relies of :IsA("").

1 Like

My bad, I was mistaken on your intent.

2 Likes

You can optimize this code a lot so I’ll actually rewrite this anyway lol, but the tween info is useful. I don’t recommend constantly reconnecting events, this would probably cause memory leakage.

2 Likes

I don’t understand what you mean by constantly reconnecting events.

1 Like

Every second, you’re constantly adding another Touched connection. Don’t you think that’ll start taking up a lot of memory after a while?