|
| 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 |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 21 Jan 2012 11:40 AM |
| children is deprecated, but thanks. |
|
|
| Report Abuse |
|
|
|
| 21 Jan 2012 11:42 AM |
| By the way, I'm mostly hoping for examples of GetChildren. |
|
|
| Report Abuse |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 21 Jan 2012 11:56 AM |
| Query: Can one put a for loop inside of a while true do loop? |
|
|
| Report Abuse |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
| |
|