Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How does OnTouch Work?

I've always wonder how the OnTouch Function works in Roblox Lua. Could you write out the script and explain each line of code? Thx.

1 answer

function OnTouch(hit) -- The start of the function, hit or anything else within those brackets is used for 
-- interacting with the player that activated the function.

-- Code goes in here, for example, hit.Parent.Humanoid.Health = 0

end -- Ends the function

script.Parent.Touched:connect(OnTouch) -- In this example the script is inside a brick that you touch,
-- 'Touched' an action that triggers the function, connect is like connecting to the "function service" -- --- (doesn't exist) and OnTouch is the function your triggering.

FYI, functions only work when they are in the same script as the trigger is in.

This discussion is preserved as it was. New answers, comments, and votes are closed.