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

How does OnTouch Work?

Asked by 10 years ago

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

Log in to vote
-1
Answered by 10 years ago
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.

Ad

Answer this question