Checking All Characters For Objects

local Players = game:GetService("Players")
while true do
	wait(0.1)
	for _, Player in pairs(Players:GetPlayers()) do
		if Player.Character:FindFirstChild("HumanoidRootPart"):FindFirstChild("Stick") then
			if game.Workspace:FindFirstChild("Stick") then
				game.Workspace:FindFirstChild("Stick"):Destroy()
			end
		end
	end
end 

So basically Im trying to see if an object exists in the workspace and in any of the games 
humanidrootparts. The issue is, the checking for the humanoid root part isnt working. Is it possible to 
check all Characters for objects? I just want to destroy the extra stick if someone already is possessing 
it in their humanoidrootpart.
1 Like
for i, plr in pairs(game.Players:GetChildren()) do
       local character = plr.Character
       -- you stuff here
end

I hope this will help

(i run it 3 time)

1 Like

Wait so that will check ALL characters for something?

Or at least GET all characters so i can check. Ex if character:FindFirstChild(“…”) then …:Destroy()?

for i,v in pairs (workspace:GetDescendants()) do
if v.Name == "Stick" then
  v:Destroy()
end
end

this script will destroy the stick, by checking EVERYTHING in workspace, including the player’s character’s humanoid root parts.

1 Like

Well, this should by work

for i, plr in pairs(game.Players:GetChildren()) do
       local character = plr.Character
       for _, obj in pairs(character:GetChildren()) do
           if obj.Name == "Humanoid" then
               print("obj with the name of Humanoid found")
           end
       end
end

1 Like

I see! So thats how you use i, v in pairs ight got it! Thank you all so much! Ill just check the with the first script to see if anyone has the stick and if so then for i,v in pairs (workspace:GetDescendants()) do
if v.Name == “Stick” then
v:Destroy()
end
end

That wont error if there isn’t in fact a stick in the workspace, correct?

	wait(0.1)
	local character = plr.Character
	for _, obj in pairs(character:GetChildren()) do
		wait(0.1)
		if obj.Name == "HumanoidRootPart" then
			print("ObjWithHRPFound")
			for i,v in pairs (workspace:GetDescendants()) do
				wait(0.1)
				if v.Name == "Stick" then
					v:Destroy()
				end
			end
		end
	end
end So this should work?

wait no i gotta check for the stick in the hrp

Nope, the if statement will not function if there is no Stick anywhere in workspace.

1 Like
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
				
			
			print("ObjWithHRPFound")
			for i,v in pairs (workspace:GetDescendants()) do
				wait(0.1)
				if v.Name == "Stick" then
					v:Destroy()
					end
				end
			end
		end
	end
end``` So this will check if there are two sticks and if so it destroys the workspace one?

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