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
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
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.
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.
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?
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.
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.
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.