For loop only returning 1 child instead of all?

I was making a script to loop through everything in workspace, but it only gets the baseplate.
There are others things in the workspace.
Like a spawnlocation and the characters.

But it only prints “Baseplate”

Code (Server Script)

local function FindTarget()
	
	local target
	local maxDis = maxDistance
	
	for _, plr in pairs(workspace:GetChildren()) do 
		print(plr.Name)
		if plr then

			if plr:FindFirstChildWhichIsA("Humanoid") == nil then return end
			print(plr.Name)
			if requireteam then
				
				if not teamkill then
			
					local team = plr:FindFirstChildWhichIsA("Team")
					if team ~= nil and team.TeamColor ~= script.Parent.Team.TeamColor then
						local distance = (hrp.Position - plr.HumanoidRootPart.Position).Magnitude 
			
						if requiresSight then
					
							if distance <= maxDis and CanSeeTarget(plr) then
								target = plr
								maxDis = distance
							end
					
						else
					
							if distance <= maxDis then
								target = plr
								maxDis = distance
							end
					
						end				
					end
					
				else
					
					local team = plr:FindFirstChildWhichIsA("Team")
					if team ~= nil then
						local distance = (hrp.Position - plr.HumanoidRootPart.Position).Magnitude

						if requiresSight then

							if distance <= maxDis and CanSeeTarget(plr) then
								target = plr
								maxDis = distance
							end

						else

							if distance <= maxDis then
								target = plr
								maxDis = distance 	
							end

						end				
					end
				
				end
			
			else

				local distance = (hrp.Position - plr.HumanoidRootPart.Position).Magnitude

				if requiresSight then

					if distance <= maxDis and CanSeeTarget(plr) then
							target = plr
							maxDis = distance
					end

				else

					if distance <= maxDis then 
							target = plr
							maxDis = distance
					end

				end				
				
			end
			
		end	
	end
	
	return target
	
end

Nothing other than baseplate…
Screenshot 2024-11-04 214922

1 Like

should not be a return, but instead a continue

1 Like

Thanks, didn’t expect it to be something so small.

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