Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can Someone Break This Code Down For Me?

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

✓ Accepted answer

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 comment

1

Dang, thanks so much.

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.

2 comments

1

:( Why the downvote? I've explained how it works.

1

This guy's just a jerk. He doesn't deserve our help.

This discussion is preserved as it was. New answers, comments, and votes are closed.