generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: GetChildren() and others

Previous Thread :: Next Thread 
jtfriend137 is not online. jtfriend137
Joined: 19 Jul 2010
Total Posts: 1960
21 Jan 2012 11:35 AM
Hello. Could anyone tell me specificly how methods like GetChildren, FindFirstChild, and GetPlayerFromCharacter work, by giving examples of each? (Atleast an example of one, anything helps!)
Report Abuse
jessemccullough9292 is not online. jessemccullough9292
Joined: 10 Jul 2008
Total Posts: 590
21 Jan 2012 11:38 AM
z = game.Players:children()
for x = 1,#z do
if z[x].Name == "jtfriend137" then
z[x];Remove()
end
end
Report Abuse
jessemccullough9292 is not online. jessemccullough9292
Joined: 10 Jul 2008
Total Posts: 590
21 Jan 2012 11:38 AM
Correction:

z = game.Players:children()
for x = 1,#z do
if z[x].Name == "jtfriend137" then
z[x]:Remove()
end
end
Report Abuse
jtfriend137 is not online. jtfriend137
Joined: 19 Jul 2010
Total Posts: 1960
21 Jan 2012 11:40 AM
children is deprecated, but thanks.
Report Abuse
jtfriend137 is not online. jtfriend137
Joined: 19 Jul 2010
Total Posts: 1960
21 Jan 2012 11:42 AM
By the way, I'm mostly hoping for examples of GetChildren.
Report Abuse
Darkmist101 is not online. Darkmist101
Joined: 30 Jul 2010
Total Posts: 6208
21 Jan 2012 11:43 AM
FindFirstChild(Name, Bool)

Finds the first child of the object;
with a matching name.

If not,
returns false.

If the Bool is true,
looks though the entire object.

Ex) game:findFirstChild("Workspace") --> Workspace
Ex 2) game:findFirstChild("Model", true) --> Model


GetChildren(Object)

Returns a table,
with all of the objects children.

Ex) for _, v in pairs(game:GetChildren()) do
print(v)
end --> Workspace, Lighting, ect


GetPlayerFromCharacter(Character)

Finds the Player's folders,
from the character specified.

Ex) game.Players:GetPlayerFromCharacter(game.Workspace.Darkmist101) --> game.Players.Darkmist101
Report Abuse
darkkiller5555 is not online. darkkiller5555
Joined: 22 Nov 2009
Total Posts: 6359
21 Jan 2012 11:45 AM
:GetChildren()

This method basically gets all the children of a specified object and returns it in form of a table.

There are 2 ways to use GetChildren(), a normal for and a generic for. A generic for is recommended.

--A normal for loop--

local children = workspace:GetChildren())
for i = 1, #children do
print(children[i].Name)
wait(.05)
end


This will print the name of every object in workspace.

--A generic for--

for i, v in pairs(workspace:GetChildren()) do
print(v.Name)
wait(.5)
end

This will also print the name of every child in workspace. The 'i' in this loop stands for the index number in the table. The v stands for the actual object.


:FindFirstChild()

FindFirstChild does exactly what it says. It finds the first child that has the specified name.

For example..

if workspace:FindFirstChild("NameOfModel") then
print("The model is here!")
end

That checks workspace for a model named 'NameOfModel' and if it's there, it prints 'The model is here!'

Remember, FindFirstChild() only looks for 1. If you have 2 objects with the same name and you use FindFirstChild(), it'll only return one of them.

:GetPlayerFromCharacter()

I do not use this. It never stuck with me.

script.Parent.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
player:Destroy()
end
end
end)

That kicks a player from the game if the player touches the part.

:GetPlayerFromCharacter is a method of the data model Players. The arguement in between the parenthesis is the Character.

Instead if GetPlayerFromCharacter, I use this.

script.Parent.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:FindFirstChild(hit.Parent.Name)
if player then
player:Destroy()
end
end
end)

It works the same. I prefer the 2nd one because it's my form of coding.
Report Abuse
jtfriend137 is not online. jtfriend137
Joined: 19 Jul 2010
Total Posts: 1960
21 Jan 2012 11:56 AM
Query: Can one put a for loop inside of a while true do loop?
Report Abuse
darkkiller5555 is not online. darkkiller5555
Joined: 22 Nov 2009
Total Posts: 6359
21 Jan 2012 11:57 AM
Yes.

while wait(5) do
for i = 1, 10, .5 do
print(i)
wait(.05)
end
end
Report Abuse
jtfriend137 is not online. jtfriend137
Joined: 19 Jul 2010
Total Posts: 1960
21 Jan 2012 12:08 PM
I've been working on a script, thanks for the help with it. I just got it to work, and It didn't work so many times before now when it worked I came close to falling out of my chair! XD
Report Abuse
darkkiller5555 is not online. darkkiller5555
Joined: 22 Nov 2009
Total Posts: 6359
21 Jan 2012 12:12 PM
No problem.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image