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

Which method is better for connecting events and functions?

This is kinda embarrassing that I'm asking this question at this stage of scripting but I kinda wanna know which method is better.

Method 1: Directly linking a function to an event

local part = game.Workspace.Part

part.Touched:Connect(function()
	-- Do stuff
end)

Method 2: Linking a already created function to an event.

local part = game.Workspace.Part

local function Touched()
	-- Do stuff
end

part.Touched:Connect(Touched)

Would love to know because I really do want to improve the quality of my scripts.

1 answer

✓ Accepted answer

To be honest, I use the first method a lot more than I do with the second. Let me tell you multiple reasons on why to use both. If you are creating a function that you want to be called multiple times, use method 2.

local part = script.Parent

local function Touch()
	print("Whatever")
end
part.Touched:Connect(Touch)

That's if you need to call the same functions multiple times, use method 2(above).

If you just want something easy for single use, just use method 1.

local part = script.Parent

part.Touched:Connect(function()
	print("Blah blah blah")
end)

SO overall, you should use both methods, I personally use method 1 more than I should, but hey, your choice. Please note that I number the methods in the order of which you did.

1 comment

0

Thanks so much! I'll keep this mind whenever I'm making scripts.

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