Help with parameter example

Hi can someone help me on this. This is from the roblox documentation page of parameters. I just don’t get how this parameter works since how would the program know waht “objectTouched” is when the function was being called in the connect event, an argument wasn’t passed in the parameter?

A .Touched event passes a parameter automatically, and that parameter is the part that touched. By connecting it to the function, it will pass any parameters straight to that function. Therefore, the part parameter passed by part.Touched will get passed to onTouch.

local function onTouch(hit)
    print(hit) --this will print the part that touched
end

Part.Touched:Connect(onTouch)
--the Roblox internal scripts will run the onTouch function passing the part that touched as a parameter
1 Like

Whenever the signal is fired, the function being used will recieve the argument for further use, which is the equivilent of saying this in code:

-- we have a function that we are going to use.
local function sayThis(text) -- parameter
    print(text); -- print this using parameter
end

-- lets say something has happened in the code where this is important
-- we choose to fire this function with some text
sayThis("Hello!") -- this string is passed to the function for it to use
-- it will then print out "Hello!"
-- However if you choose to not give it anything, like this:
sayThis() -- it will print out "nil" as it has no value.

Basically, the signal will detect something happening, and then send out the relevent data using the function that was connected to said event, sometimes it will not give you anything in which any parameter would be nil.

1 Like

thanks for the help! i understand now

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.