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
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.