Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Difference between return end and end? [closed]

Asked by
Neon_N 104
5 years ago

Here is a example script. I will call it script one.

local UserInputService = game:GetService("UserInputService")
local Key = 'Q'
UserInputService.InputBegan:Connect(function(Input, IsTyping)
    if IsTyping then return end
local KeyPressed = Input.KeyCode
    if KeyPressed == Enum.KeyCode[Key] and Debounce and Char then
        Debounce = false
        local LoadAnimation = Char.Humanoid:LoadAnimation(Animation)
        LoadAnimation:Play()

And this is another example. Script two.

game.Workspace.Player.Humanoid.Running:Connect(function(speed)
    if speed > 0 then
        print("Player is running")
    else
        print("Player has stopped")
    end
end)

Why did the scripter not use "return end" on script two? They just used end right after print.

0
In the first example the scripter just wanted to return the value end, and in the second they are ending the script. Synth_o 136 — 5y
0
Well that is how I think about it Synth_o 136 — 5y

Locked by User#24403, DinozCreates, and Amiaa16

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

2 answers

Log in to vote
5
Answered by 5 years ago
Edited 4 years ago

here is a piece of confusion in this question.

"Difference between return end and end?"

You ask what the difference is between return end and end. However, this alone isn't really a meaningful question.

end is a keyword used to signify the end of an if, while, for, and function scope.

Return

If you do not understand how the return keyword works, I will explain. If you do already, you can skip this.

What it does is either return a value from a function, prematurely end a function, or both.

Returning values

local function getSum(x, y)
    return x + y -- returns the sum of x + y
end

local sum = getSum(8, 25)

The sum variable holds the "result" of the function. In this case the result is 33, because the function returns the sum of two numbers. Not all functions have return values. Another example of a function that has a return value is wait. Now this function has two return values; the delta time (time waited) and the time since the program started.

local dt, timeSinceProgramStarted = wait(3)

print(dt, timeSinceProgramStarted) 
--> 3.00038384925, 3838.9 (just an example, you will get different numbers)

Prematurely ending functions

You can prematurely end a function, if, for example, a condition is NOT met.

local pizzaPrice = 15
local function orderPizza(customer)
    if customer.Money.Value < pizzaPrice then 
        return -- return out of the function if they do not have enough money
    end

    -- Function won't even reach here if the condition above was met.

    customer.Money.Value = customer.Money.Value - pizzaPrice
    -- give them their pizza
end

Answer

If a function reaches a return, the entire function stops. The developer probably didn't want the entire function to stop.

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

for example, u can do

if thing ~= nil then print("thing is nil") end

instead of

if thing ~= nil then
    print("thing is nil")
end

then return end should be fashioned similarly:

if thing ~= nil then return end

instead of

if thing ~= nil then
    return
end

im sorry if i got ur question wrong im also not rly sure what im doing; but i believe return end is just something ppl like to do; like

local function thing()

end

instead of

function thing()

end

although they are pretty much the same

0
although i might be wrong TheluaBanana 946 — 5y
0
yes this is wrong User#24403 69 — 5y