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