Getting the children of all Players

Hi there,
I’m trying to create a little script where it checks for a certain value in every single player’s Children. I tried this, but it didn’t work:

for i,v in pairs(game.Players:GetChildren():GetChildren()) do
	if v:FindFirstChild("Active") then
		v.Value = true
	end
end

Can someone please help me with this? Thanks!

2 Likes

try v.Active.Value = true

for i,v in pairs(game.Players:GetChildren():GetChildren()) do
	if v:FindFirstChild("Active") then
		v.Active.Value = true
	end
end

– i’ve never personally tried GetChildren():GetChildren(), if it doesn’t work loop the players then loop the individual player like this

for _,player in pairs(game.Players:GetPlayers()) do
for _, stuff in pairs(player:GetChildren()) do
if stuff.Name == "Active" then
stuff.Value = true
end
end
2 Likes

The GetChildren method does not return the child, it returns a table. If you just want to check if it exists in any of the player’s children then just do

local players = game:GetService("Players")

for i, player in pairs(players:GetPlayers()) do --Get the players and go through each player
    for i, descendant in pairs(player:GetDescendants()) do --Get the descendants of the player
        if descendant.Name == "Active" then
            descendant.Value = true
        end
    end
end

Getting Descendants of the player object will result in going through unnecessary objects, while this could be one way to achieve what the OP is requesting, it’s not a good practice to get descendants that you will not use in the first place.

2 Likes

None of this appears to be working. What I’m trying to do is to have the script check for a value in a player’s Children and set it to true. This will activate a GUI-based system. I’ve tested all suggested scripts to no avail. :frowning:

Could you be more specific when you say your code isn’t working. Is the value not getting set to true? Is the output showing an error?

Is this the only thing in your server script? If so, the code will run when the server starts, which is almost always before a player exists in the game. Can you share the place file?

The value is not getting set to true.

Could you provide what the parent of Active is? Is the parent the player object? Or is it somewhere else inside the player?

The parent of Active is the player. If you need, I can share a baseplate file containing the script(s) and GUI.

There’s a second property to the FindFirstChild function which allows you to search into the children of an object.

for _,plr in pairs(game.Players:GetPlayers()) do
   local active = plr:FindFirstChild("Active", true) 
end

Also, the problem with your initial choice is that you’re trying to use the GetChildren function (which returns a list) on a list;

list = game.Players:GetChildren() -- {plr1, plr2, etc...} 

list:GetChildren() -- a list doesn't have this function. 
--Get Children is only a member of DataModel (which parts, models, lights, guis etc inherit from) 

One last thing. If you’re changing this property using a LocalScript, the server won’t know about it.

2 Likes

Could you try this code:

for i, v in ipairs(game.Players:GetChildren()) do
    if v:FindFirstChild(“Active”) then
        v.Active.Value = true
    end
end

Also when is this code ran? On server start or at a specific time?

Edit:
You could pm me the file and I’ll take a look

As others mentioned, you cannot use GetChildren on a table.

for i,v in pairs(game.Players:GetChildren()) do
    for a, b in pairs(v:GetChildren()) do
        if b.Name == “Active” then
            b.Value = true
        end
    end
end

The OP’s request has been solved, the issue was the Active object was being created on the Client instead of the Server.

4 Likes

Would be nice if OP marked a solution or let us know about the problem being solved then. I feel like a lot of the help being offered here was rudely ignored.

1 Like

So far I’ve tried many of the code offered up by the people, and they don’t appear to be working. Like @wevetments said, it’s because that I created the values using a LocalScript, so the server dosen’t know about such values. I’ll fix that up now.