How would I loop a GetDescendants() loop?

when u clone the parts, add a touched function to it.

There is no need to use :FindFirstChild() all those times when you don’t check if the child exists in the first place. Also, unless you add the spawn location during runtime, you don’t need to use :WaitForChild() either.

My best guess is that your script’s loop runs once and never again. You probably don’t even need to use a loop when you can use the .DescendantAdded event.

I have shown a .DescendantAdded() event above, if you could check it out and tell me what is wrong that would be great!

Interesting. The script itself doesn’t have anything wrong with it. Are there multiple instances named “Ending”?

1 Like

No.

There is only one map cloned and placed into the workspace at a time. Inside of this map is a single part named “Ending”. Is this what you mean?

My only guess is that the ending part isn’t cloning properly.

I have just tested the script (my original) script without the cloning of the map. (The map is already in workspace, in other words). The code works, and I get teleported just fine. So my guess it that the loop only searches through the workspace descendants once, and that doesnt buy time for the map to clone in.

1 Like

Put this script in the script which clones the map and run it after the clone. Otherwise you’ll need a wait or trigger of some sort.

Also, you could have figured this out a lot faster if you just use print statements for debugging:
You can easily see that the loop is running, but that its not finding the part. Without prints you basically know nothing.

image

1 Like

The cloning script is a local script. The even won’t fire since it is a FireClient() command. What should I do?

Also, I think I previously did this, and it didn’t work.

IDK, but if its a localscript and multiple players are cloning this in, your server script would be making these connections over and over again(duplicates). Time to rethink your design?

I only want the map to clone for one player, hence the client usage.

What if you put the script inside the ‘map’ then it will run when it is added to workspace.

for i,v in pairs(game.Workspace:GetDescendants()) do
	if v:IsA("BasePart") then
		v.CastShadow = false
	end
end

for i,v in pairs(game.Workspace:GetDescendants()) do
	if v:IsA("BasePart") then
		v.Touched:Connect(function(hit)
			if v.Name == "Ending" and v:IsA("BasePart") then
				if hit.Parent:FindFirstChild("Humanoid") then
					-- Do Stuff
				end
			end
		end)
	end
end
1 Like

@Crrustyy Try this script! Put it in ServerScriptService

1 Like

@Crrustyy Oops! I made a mistake in the script, please use it again, it has been updated.

Tested this and all works. I think problem in function touched or workspace has a lot of instances and you touching part before loop went to it.

His problem is that the parts in question do not exist in the workspace at the time he is looping through the workspace. He needs to wait until the ‘map’ is cloned before running the loop. I suggested one way might be to put the script in the map model that is being cloned, that way the server script will run after the ‘map’ is cloned into the workspace.

The code that I gave works, when you touch a part in the workspace and it is called “Ending”, then it’ll do something.

However, if it’s cloned, it won’t be originally there. You can’t do :WaitForChild(), it’ll infinite yield, I’ll make code for that now.

1 Like