0

I've tried really hard to understand how parameters work in Lua, but I didn't understand yet.

This is de code: It basically kill someone when touched.

function onTouch(part)
    
    local player = part.Parent:FindFirstChild("Humanoid")
    
    if(player ~= nil) then
        player.Health = 0   
    end
    
end

script.Parent.Touched:Connect(onTouch)

My question is, how "part" is a valid parameter? What the Script does to use part as a parameter?

In my mind there is no sense, part is not even defined, and this Script works.

Thanks so much!

1 Answers1

0

A parameter is a local variable whose initial value is an argument passed to the function when it's called. Parameters are declared as part of the function declaration.

I don't know Roblox, but in this code, it seems that onTouch is a callback: The function gets stored by script.Parent.Touched:Connect(onTouch) and later gets called somewhere we can't see, similarly to onTouch(myPart).

luther
  • 5,195
  • 1
  • 14
  • 24