Workspace:GetDescendants() is only returning the camera instance

Workspace:GetDescendants() is only returning a camera instance and I cannot figure out why.

Here is my script (on the client), I cannot seem to figure out why this script is not working. It’s located in StarterGui so it would be running under the player’s PlayerGui as a local script.

local UserInputService		=		game:GetService("UserInputService")
local PlayerService			=		game:GetService("Players")
local RunService			=		game:GetService("RunService")
local TweenService			=		game:GetService("TweenService")

---

local Player		:	Player			=		PlayerService.LocalPlayer
local Camera		:	Camera			=		workspace.CurrentCamera
local Info_Folder	:	Folder			=		Player:WaitForChild("Info")
local Character		:	Model			=		Player.Character or Player.CharacterAdded:Wait()
local Humanoid		:	Humanoid		=		Character:WaitForChild("Humanoid")

---

--


local Parts = {}

RunService			.	RenderStepped	:		Connect	(function(dT)

	if not Info_Folder:GetAttribute("AutoAim") then return end
	
	for i, V_1 in pairs(workspace:GetDescendants()) do
		
		print(V_1)

		--- Irrelevant functions past this point
		if not V_1:IsA("Model") 							then return end	print("model")
		if not V_1:FindFirstChildOfClass("Humanoid") 	then return end print("humanoid")

		print("hi")

		

		local Position 		= Camera:WorldToViewportPoint(V_1.PrimaryPart.Position)
		local Center	 	= Camera.ViewportSize / 2

		table.insert(Parts, {
			Block 		= V_1;
			Distance 	= (Center - Vector2.new(Position.x, Position.y)).Magnitude
		})
		
		
	end

	table.sort(Parts, function(a, b)
		return a.Distance < b.Distance
	end)

end)

Here’s what is supposed to show up in the workspace. I want the script to print the closest character to the model, however I believe that is not relevant and my scripting is correct- it is just the :GetDescendants() that does not seem to function properly but I have attached it incase it might have function.

You should use continue instead of return. The code structure currently works like this:

  1. Looks at first descendant
  2. Is it not a model? Return

Nothing else will happen after you’ve returned. This is where continue comes into play! It’ll continue the iteration instead of stopping and returning.

The camera is coming from :GetDescendants()

Line 24:

image

Yeah and that’s expected to be printed on RenderStep. Try replacing return with continue and you will see different results.

The reason why it stops at camera is because after your print it checks to see if the camera is a model, which it is not.
RobloxStudioBeta_yuG1O2HQeY

Because it’s not a model, it will return. Nothing else beyond that will happen until the next render step, in which it will fail again since camera is again not a model.

1 Like

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