Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I get all hats?

Asked by 9 years ago

I was wondering if anyone could fill me in. How would I get the children of the character. But only destroy Hats. I know how to get the name of something through :GetChildren() But how do I get the Hat object?

1 answer

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

All Instances have a ClassName property which is equal to what the instnace is. No matter what you rename an object its ClassName will always be the same. A Part is always a Part, even if you were to rename it "BoolValue". It is a Part, it always will be a Part, and it's ClassName will always be "Part."

Using this, we can check if a player has a Hat.

for i,v in pairs(character:GetChildren()) do --Make sure character is defined somewhere. 
    if v.ClassName == "Hat" then
        --code
    end
end

EDIT:

i is the index, and v is the value. This is most easily seen if you print them. For example, run this code:

for i,v in pairs(workspace:GetChildren()) do
    print(i.."is the current index!")
    print(v.."is the current value!")
end

This for loop loops through all the children of workspace. For example, let's say you have three parts in workspace. The first time the loop iterates, v equals the first part. Then the second time the loop iterates, v equals the second part, and so on. i is a number, like 1, 2, 3. If i equals 2, then you know that the loop already iterated one time, and is working on its second.

But really, you'll understand the best if you try the code above with the prints.

0
Can you tell me what i and v represent? I see them all the time in this loop BosswalrusTheCoder 88 — 9y
0
i represents 1, and v represents the object that is being accessed. SquirreIOnToast 309 — 9y
0
Read it now Perci1 4988 — 9y
Ad

Answer this question