Help with parameters in roblox lua! Please

This is a parameter:

function athing(input)
  --code
end

And this is the argument:

athing('Output')

However when i come to something like this: (I copied the whole script from the devhub , so don’t ask questions.)

local function killPlayer(otherPart)
    local partParent = otherPart.Parent
    local humanoid = partParent:FindFirstChild("Humanoid")
    if humanoid then
        humanoid.Health = 0
    end
end
 
lava.Touched:Connect(killPlayer)

This is where things are too confusing. (Sorry if i am becoming unclear now.)

I may be able to figure out this part:

local function killPlayer(otherPart)
   
end

However when i come to this:

otherPart.Parent

Thats just where i am not getting it: otherPart.Parent

Like what is otherPart.Parent?

I kept reading on the devhub but that did not change anything.

What is the point of this?

Heres an example.

In this example the Parent of Baseplate is Workspace,
The Child of Baseplate is Folder,
And Folder is an Descendant of Workspace
image

If you imagine everything as containers, the parent of otherpart is simply what ‘otherpart’ is inside (most likely the character which is a model)

When an Instance is passed through a function’s parameters, the parameter itself becomes a reference to said Instance.

--Example

local function printInstanceName(object : Instance)
   print(object.Name)
end

local newInstance = Instance.new("Part") -- Create a new Part
newInstance.Name = "NewPart" -- Set its name

printInstanceName(newInstance) -- prints "NewPart"

What do you mean by that? (Sorry i am not following up here)

In Luau, when you pass an Instance through a function, whatever parameter is tied the instance will turn into a reference for it (it will act as a local variable for the object inside the function).

That might sound a bit confusing, so I’ll explain it using the example you used.


local function killPlayer(otherPart)
    local partParent = otherPart.Parent
    local humanoid = partParent:FindFirstChild("Humanoid")
    if humanoid then
        humanoid.Health = 0
    end
end
 
lava.Touched:Connect(killPlayer)

BasePart.Touched is a RBXScriptSignal that passes a reference of the object that touched the Instance that the signal is connected to.

When connecting a function to the signal, all the arguments sent through the RBXScriptConnection are passed through the function, including the object that touched the Instance.

otherPart.Parent is getting the parent of the BasePart that touched the Instance.


TL;DR It’s the same as doing:

lava.Touched:Connect(function(otherPart)
    --otherPart was sent from the signal
    local partParent = otherPart.Parent
    local humanoid = partParent:FindFirstChild("Humanoid")
    if humanoid then
        humanoid.Health = 0
    end
end)

Does that mean otherPart.Parent is:

Leftfoot.Parent

If Leftfoot was the object that touched the Instance, then yes

If we printed otherPart, it would print everything that touched it. Wow! Lua got more interesting.

I am still wondering why Connect is accessed using “:” instead of “.”

Data:Method is the same as:

Data.Method = function(self, ...)

end

or

function Data.Method(self, ...)

end

Roblox most likely uses self internally

Hey wait! Isn’t :Connect a function?

Yes, it returns a RBXScriptConnection, but I also recommend starting a new topic for any further questions about :Connect to prevent being off-topic

1 Like