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

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

Asked by
vinboi 0
8 years ago

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

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
8 years ago

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

Ad
Log in to vote
-1
Answered by
Master_JJ 229 Moderation Voter
8 years ago

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.

Answer this question