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

What does this parameter in this function do/purpose/refer to?

Hi, so I'm currently doing this Wiki tutorial - http://wiki.roblox.com/index.php?title=Making_an_Explosion_Course

So in the workspace, there are 5 parts named "ExplodePart" and the goal is to make them all explode.

[The problem:] I am having trouble understanding parameters and its role in this code below. For example in line 1 "function ExplodePart(part). What does (part) represent or mean? What does it do? What is it referring to in the workspace?

[CODE:]

function ExplodePart(part)
	local explosion = Instance.new("Explosion", game.Workspace)
			explosion.Position = part.Position
end


children = game.Workspace:GetChildren()
while true do
	for _, child in ipairs(children) do
		if child.Name == "ExplosionPart" then
			ExplodePart(child)
		end
	end
	wait(1)
end

2 answers

✓ Accepted answer

The part in the first line is a parameter. Technically, it is an upvalue, which is a local variable defined at the time the function is called.

function ExplodePart(part)
    local explosion = Instance.new("Explosion", game.Workspace)
            explosion.Position = part.Position --Notice this line. It assume 'part' has a Position property.
	--Part is just a variable - in this case, it could point to *any* part in Workspace. We choose which when the function is called...
end


children = game.Workspace:GetChildren()
while true do
    for _, child in ipairs(children) do
        if child.Name == "ExplosionPart" then
            ExplodePart(child) -- And it is called right here.
		--Child is defined by the generic foor loop.

		--I should mention that this code is bad practice, because Workspace can update and `children` won't due to it being defined outside of the loop
        end
    end
    wait(1)
end

Each event has different parameters. The parameter connected to the touched event is the part that touches it. For example, if you type the following code:

local part = game.Workspace.Part

part.Touched:connect(function(part)
	print(part.Name)
end

The code above prints the name of the part that touches the part that is in the workspace. If you walk on the part, and your left leg hits it, in the output, it would say, "Left Leg"

To sum it all up, the parameter is the thing that touches the part the script is connected to.

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