How does "end" work in lua?

Dodobee

Member
Using end confuses me as I don't understand when to put the bracket at the end of the script and it always gives me errors saying unexpected symbol near ')'
 
it's used to "close" every block you start.

end:
--end closes "if" blocks
if (something) then

end

--end closes "if-else" blocks
if (something2) then

else

end

--end closes function blocks
local function test()
    print("henlo stinky")
end

--end closes for-loops
for val in pairs(sometable) do

end

Just make sure you're matching every "block" you start with exactly one "end"
 
There is no end with the bracket. You might be thinking of the time when you put an entire function in an addHook.

Lua:
addHook("ThinkFrame", function()
    print("test")
end)

This is a scenario where a bracket might follow an "end". The bracket is part of the function, not the code block itself.

Lua:
local function testFunction()
    print("test")
end

addHook("ThinkFrame", testFunction)

That code is exactly the same as this code. I recommend using the second one, it's much cleaner and doesn't get you tripped up about brackets.
 

Who is viewing this thread (Total: 0, Members: 0, Guests: 0)

Back
Top