Checking All Characters For Objects

use spawn functions

spawn(function()
while wait(0.1) do
for _, obj in pairs(character:GetDescendants()) do
		if obj.Name == "Stick" then
			print("Found Stick inside".. obj.Parent.Name)-- that sounds so wrong lol
end
end
end
end)



spawn(function()
while wait(0.1) do
for i,v in pairs (workspace:GetDescendants()) do
if v.Name == "Stick" then
  print("the workspace check has found a Stick inside"..v.Parent.Name)
end
end
end)
1 Like

Hmm? Whats a spawn function? .

If you expect the stick to be in the workspace with it being it’s parent and you expect the stick to be in a character’s humanoid rootpart, you don’t need to use GetDescendants() on the workspace at all.

1 Like

Just find first child? I mean i didn’t know

Yes, Players:GetPlayers() returns a list/table of players in the game, if you just need to check if the humanoidrootpart has a stick, you just go through that list and use FindFirstChild to see if there’s a rootpart and also check if there’s a stick and if there’s a stick you can use FindFirstChild on the workspace to get rid of the stick.

What FindFirstChild does is check if there is something called what you’re calling for right under the parent.

1 Like

Oh ok I see!! :smiley: Thank you so much let me test it out rq.

It isn’t working. The issue is checking if the stick is in any of the characters humanoid root parts.

GetDescendants would check both the characters and the workspace in one ‘for i,v’ loop though?

1 Like

I just want to check ALL of the players humanoid root parts for the child stick. If you could give me the script it would be much appreciated.

Cause i really dont get it. I tried the i,v loop n stuff i jsut dont get it.

What’s your new code @BMWLux ?

@DybalaplaysYT GetDescendants would get everything in the workspace that’s inside it, I think that’s unnecessary if the OP knows that a stick is going to be in the workspace and he only wants to check what’s inside a character’s humanoidrootpart

1 Like

but if you look at the original post, he said he wants to check if the object exists in the workspace and in any of the games humanoidrootparts

So basically Im trying to see if an object exists in the workspace and in any of the games 
humanidrootparts.

and GetDescendants() can do all of this in one thread

1 Like

this is the simplest i can do

for i,v in pairs (workspace:GetDescendants()) do

 if v.Name == "Stick" then
  print("Stick found")
 end

end

since the player’s Character HumanoidRootPart are descendants of workspace, It will check their HumanoidRootParts too

1 Like

it will? But like it will also find the workspace. How can I check if there is two of them and if so then delete one.

for i, plr in pairs(game.Players:GetChildren()) do
wait(0.1)
local character = plr.Character
for _, obj in pairs(character:GetChildren()) do
wait(0.1)
if obj.Name == “HumanoidRootPart” then
if character.HumanoidRootPart:FindFirstChild(“Stick”) then
game.Workspace:FindFirstChild(“Stick”):Destroy()

		print("ObjWithHRPFound")
		
		end
	end
end

end

But like is this possbile? If so how could I do it?

I think this script would help, did a boolean check if a rootpart has a stick and then it will check to destroy a stick in a workspace.

local Players = game:GetService("Players")
while true do
	local FoundStickInCharacter = false
	for _, Player in pairs (game.Players:GetChildren()) do
		if Player and Player.Character and Player.Character:FindFirstChild("HumanoidRootPart") then
			local RootPart = Player.Character:FindFirstChild("HumanoidRootPart")
			if RootPart:FindFirstChild("Stick") then
				FoundStickInCharacter = true
			end
		end
	end
	if FoundStickInCharacter == true and game.Workspace:FindFirstChild("Stick") then
		print("Stick found in child of workspace")
		game.Workspace:FindFirstChild("Stick"):Destroy()
	elseif FoundStickInCharacter == true and not game.Workspace:FindFirstChild("Stick") then
		print("No stick found in child of workspace")
	end
	wait(0.25)
end
2 Likes

Fr. If this works I will heart every one of your comments XD

local foundalreaady = false

for i,v in pairs (workspace:GetDescendants()) do

 if v.Name == "Stick" and foundalready == false and v.Parent ~= workspace then
  print("Stick found")
  v:Destroy()
  foundalready = true

  elseif v.Name == "Stick" and foundalready == true and v.Parent == workspace then
   print("Found a stick that is child of workspace. Keeping Stick.")
  end

end
1 Like

Same to you lol :smiley: Muchas Gracias