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

Is my touch script working properly?

It seems to be sending a print of every part it touches, and I only want it to send a print of the handle.

workspace.TryArea1.Touched:connect(function(Handle)
  print(Handle)
end)

[UPDATE] I edited it and still not working. I only want the Handle to print TRY.

workspace.TryArea1.Touched:connect(function(otherPart)
  print 'fail'
end)

if otherPart == Handle then
	print 'TRY'
end

Thanks for your help!

1 answer

That's because "Handle" is the argument of the function, meaning that whatever touches TryArea1 will be the one manipulated by the function (if you let it).

A function's parameter (whatever is in the parenthesis) can be name as whatever you want it to be.

function Example(argument)
end

function Example2(dfjkdslfjaksda)
end

function Example3(omgomgwowwhy)
end

But, of course, if you want something done to the argument, make sure capitalization and spelling are exact.

function Example(argument)
	print(argument*2)
end

function Example2(dfjkdslfjaksda)
	print(dfjkdslfjaksda)
end

function Example3(omgomgwowwhy)
	print(omgomgwowwhy+omgomgwowwhy+omgomgwowwhy)
end

-- TO USE THESE FUNCTIONS FOR A SPECIFIC PURPOSE --
-- You can place the argument you want in to the parameter (the things in between the parentheses)

function RelayTheExamples()
	Example("I like cookies.")
	Example2("What's 9 + 10?")
	Example3(7)
end
script.Parent.Touched:connect(RelayTheExamples) -- 'Touched' is one of the plethora of events Roblox provides

OUTPUT:

I like cookies.

What's 9 + 10?

21

If you want to only send a print of the handle, use an 'if' statement.

workspace.TryArea1.Touched:connect(function(Handle)
	if Handle.Name == "Handle" then
		print(Handle)
	else				-- 'else' and the next line are optional
		print("fail")
	end
end)

What the If Statement is Doing:

If the name of the handle is "Handle", then print "Handle"; otherwise, print "fail".

NOTE ON THE UPDATE

The argument can't be accessed outside of the function. "otherPart" will return an error because it is an unknown global variable.

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