How to know when to put "end" in scripts?

I have a few questions that I have at the back of my mind for a long time,

  1. When do I put end to end a function?
  2. How do I know when to put in a end?
  3. What is different from this end to end) like this?

I sometimes add end but is quite annoying to test out when to put end in your script, I sometimes paste in a section of a script and the end doesn’t automatically be added.

Sorry for the post format.

2 Likes

The intellisense will put end for you.

But there is never a time when you need to add an extra ‘)’, It looks like that because you’re passing a function to a function.

Syntax:

local function func()
    -- function
end
-- this hypothetical function called "callbackFunction" is taking a function as an argument.
callbackFunction(function()

end)
2 Likes

You use end in code blocks. That is, scopes.

do
    -- block
end

if x then

end

while x do

end

for x do

end

function()

end

The only exception to this is the repeat until, those don’t use end.

This is just passing a function to another function, so the ) after the end is just closing off the function call.

f(function()

end)

f( function() end )

Functions are first-class values, so they are treated the same as values of any other datatype. They can be passed around to functions as arguments, they can be stored in variables, etc.

4 Likes

end) is the same as end, the ) is used to close the function that you’re connecting to an event.

2 Likes

To actually understand when to put an end of a script, it is when you should exit the scope of the function; leaving the function environment. If you want certain variables outside the environment, just put them before the function, but it is usually unusual that a variable is written after functions.

Anything that incapaz mentioned is correct. The parenthesis isn’t much of a deal, it closes the existing parenthesis from the beginning.

1 Like

If you Mess Up Adding an “end” In A Script it Will be Underlined Red So that you Know there Is An Error.

Normally Once you Open parenthesis The script will Automatically Add in an “end” or “end)” for you.

In Other Cases, You Can Just Check If You Closed a function Or Not.
Suppose You Make a function

game.Players.PlayerAdded:Connect(function()

Now, You Can See You Have Opened 1 Bracket After the “Connect” Word and Havent Closed It,
Hence You Know You Need to add an “end” with a Closing Bracket.

Mark Solution if it Helped! :grinning:

1 Like