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

Can Someone Break This Code Down For Me?

Asked by 9 years ago
function onTouched(hit) 
    local d = hit.Parent:GetChildren() 
    for i=1, #d do 
        if (d[i].className == "Hat") then 
            d[i]:remove() 
        end 
    end
end 

script.Parent.Touched:connect(onTouched) 

Can someone please break this code down for me and explain how it finds the player's hats and removes them please?

2 answers

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

Looking at the "top level", there are essentially two statements:

function onTouched(hit) 
    ...
end 

script.Parent.Touched:connect(onTouched) 

A function declaration (that uses the function keyword) that defines a function called onTouched with one parameter named hit.

Second, a call to connect to the Touched event.

Events trigger on some particular stimulus, calling any function that has been passed to their connect method. In this case, onTouched, the function defined before, is passed.

onTouched will now be called whenever another part touches script.Parent. According to the Wiki article on Touched, it passes one parameter -- the other part that touched script.Parent.



Returning to the function:

function onTouched(hit) 
    local d = hit.Parent:GetChildren() 
    for i=1, #d do 
        if (d[i].className == "Hat") then 
        d[i]:remove() 
        end 
    end
end 

hit is the parameter of the other object touching script.Parent. If it's a player, it will be a Leg or Arm, Head, Torso, or perhaps the handle (the Part inside) of a Hat or Tool.


local marks the definition of a new variable, d. d is the list of the children of hit's parent.

Imagine that hit was the player's Leg. Then hit.Parent is the parent of their leg -- the Character model.

Asking for the Children of the character model would give the player's equipped Tools, Hats, and their limbs, Torso, and Head.


We loop through this list, from starting at 1 up to #d, where #d is the number of things in the list d (the number of things in hit.Parent)

d[i] is then the ith object in the list.

We check if the className, the "type" of the object, is a Hat, and in that case, remove (destroy) it.




The function could be improved much, though:

function onTouched(hit)
    local model = hit.Parent
    if model then -- its parent exists (deals with physical bullets)
        for _, child in pairs(model:GetChildren()) do
            -- "Do the following `for` each `child`"
            if child:IsA("Hat") then
                child:Destroy()
            end
        end
    end
end
1
Dang, thanks so much. alienantics 15 — 9y
Ad
Log in to vote
1
Answered by
Redbullusa 1580 Moderation Voter
9 years ago
function onTouched(hit)                             -- Your function, with "hit" as the argument.
    local d = hit.Parent:GetChildren()              -- "d" would be all of the objects within "hit.Parent"
    for i=1, #d do                                      -- This loop will run itself as many times as the number of objects there is in "hit.Parent" (d)
        if (d[i].className == "Hat") then               -- If a "d's"given object has a class name of "Hat," then
            d[i]:remove()                                   -- the given object in "d" will be removed.
        end 
    end
end 

script.Parent.Touched:connect(onTouched)        -- Triggers your onTouched function

Basically, if script.Parent is touched, the script will reference all of the objects in hit.Parent.

The "for" loop with i = 1, #d will run itself as many times as the amount of objects within hit.Parent

In each loop, the "if" statement will try and find an object with the class name of "Hat." If it does detect an object with a class name of "Hat," the given object will be removed.

1
:( Why the downvote? I've explained how it works. Redbullusa 1580 — 9y
1
This guy's just a jerk. He doesn't deserve our help. bobafett3544 198 — 9y

Answer this question