RBXScriptSignal

Tell me if this post is put in wrong category
Quick question: Which one do you think would be the best for organizing your code?
First way

game.workspace:WaitForChild(“Part”).Touched:Connect(function()
     ——Do some code here
end)

Second way

local function something()
     ——Do code here
end

game.workspace:WaitForChild(“Part”).Touched:Connect(something)

(i think this is the right category; please correct me)

I usually use the former if I don’t need to reuse the function given to Connect. I think extracting the function really doesn’t do much (i suppose for extracting a long function? i don’t see the point in that either).

I don’t think Roblox’s code examples don’t like the first way (see BasePart.Touched and Player.PlayerAdded), but there’s also this code (first one) so I don’t know what’s going on over there.

Ok bro I will fix it (Actually, I thought I had written it wrong in the first way)

The second way is needed if you want to call the method yourself, as well as it being connected to a signal. If you don’t want to call the method you’ve connected, the first way is better for organization, but they perform the same either way.

1 Like

They’re both fine btw, it’s a matter of style.

1 Like

None will work

I usually use the second option when there’s an extra level of complexity involved. For example having a list of functions that will connect to the event given a state, or a function that connects to multiple events. If something is simple in nature I tend to use the first option instead.

Uh… What do you mean? (I just don’t understand what you said)

1 Like

but responding to your question i use the second one because

In my opinion, it depends on the function itself. The first way is alright if the function is only can be connected by that action.

The second way however is better if the function are going to be called by multiple situations

First way: only if I need to run snippet of code once or if it’s in a for loop.

Second way: If I need to use the function multiple times, or in more rare cases, when I need to initialize an object with the function before it can be updated by the same function. I mostly use it in Module Scripts. An example is notifyPlayer(message, color, sound) for when I want to notify the player with a popup or something.