Defining variables?

Hey guys,
So I’m mainly a builder, not a scripter, but the new look on the Developer Hub got me intrigued to coding so I started to try it out. Im on the building a killbrick part, and there is this part that is confusing me. So basically, they are explaining parameters and I get what they are and everything. But this is the line of code that is confusing me.

local function killPlayer(otherPart)

Basically, they didnt define otherPart, and I noticed that but moved on.
Then comes up this script.

local function partParent = otherPart.Parent

So basically now they defined another part using a part that they didnt define?
I dont get it, is otherPart already a automatic defined variable?

Can someone explain?

Its more like this that goes behind the scenes

--stuff in the engine probably
while true do
    if someonepart collided with another and one of them has a touched connection
      thatfunction(the other part)
Part.Touched:Connect(function(the othe part)

end)

It is not automatically defined, just passed to your connection.

The word inside of the parentheses of the function is known as a parameter, which is essentially a placeholder for any value that is sent into the function.

For example, take a look at the following function:

local function testFunction(parameter)
    print(parameter) -- This would print Hello!
end

testFunction("Hello!")

When the function is activated, a value was sent into the function (in this case, that was the string "Hello!"). When the function receives that piece of data, it can then be referenced by the parameter inside of the function’s parenthesis. This can be done with multiple DataTypes without needing to change the parameter, as shown below:

local part = workspace.partNameHere

local function testFunction(parameter)
    print(parameter)
end

testFunction("Hello!") -- The function would print Hello!
testFunction(part.BrickColor) -- The function would print the BrickColor property of the part

However, let’s say that we send an Instance through instead. In this case, we’d be able to reference objects from its “perspective”

local Folder = workspace.folderNameHere
local part = Folder.partNameHere

local function testFunction(partThatWasSentThrough)
    print(partThatWasSentThrough.Name) -- This would print partNameHere

    local parentOfPart = partThatWasSentThrough.Parent -- This will reference what contains the part
    warn(parentOfPart.Name) -- This would print "folderNameHere"/the name of the object that contains it
end

testFunction(part) -- This will activate the function, sending the part (referenced on line 2) into it

(I added this second codeblock after re-reading and realizing I didn’t read the OP close enough, so this example should provide a better explanation for your use case. Let me know if you have any additional questions!)


More information about this can be found on the following pages:

Developer Hub Resources

Variables Article

Functions Article

NEW: Developer Onboarding


Education.Roblox Resources

Variables Tutorial

Parameters and Events Tutorial – This page has examples that include what you are trying to achieve, too!

1 Like
local player = player lol
local playerparent = player.parent

local function killPlayer(player)
      local humanoid = player.Character.Humanoid
      humanoid.Health = 0
end

i think they tried to do this

1 Like