Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

What does the function (ontouch) do?

Asked by 10 years ago

I'm learning how to script with Wiki, and it says stuff about this function. I'm very confused of what it means... Please help?

2 answers

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

"ontouch" is more likely than not just a name. Functions are, in essence, variables that contain runnable code instead of, say, a number.

You typically name functions based on what they do, or how they are activated or "called". In the case of most "OnTouch" named functions, the connected event is the Touched Event. What this means is that whenever this event "fires", which happens when the brick being referenced registers a collision, the connected function is called, with an argument passed of a reference to the colliding object.

function OnTouch(hit) --Function named "OnTouch" | Parameter 'hit' to contain the reference to the colliding object when the event fires.
    print(hit.Name) --Prints the Name of the colliding part to Output
end
Workspace.Brick.Touched:connect(OnTouch) --Connection line
--[[Touched is a member of Workspace.Brick, and it is an Event that fires whenever the physics engine determines that another part has colliding with it. The connect method of the Touched event tells the ROBLOX engine that function provided as an argument (in this case, "OnTouch") should be called when the event fires.]]

If you don't know, a method is simply a function that is also a member of an object. Members are like properties, such as "Name", but differ in that they are "child" object, rather than just a value.

Ad
Log in to vote
0
Answered by 10 years ago

It's not an actual function. Functions can be named anything, unless of course it's an inbuilt function. People just name the function 'onTouch' or 'onTouched' because they link that function with the Touched event.

Answer this question