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

Difference between return end and end?

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.

2 comments

0

In the first example the scripter just wanted to return the value end, and in the second they are ending the script.

0

Well that is how I think about it

2 answers

✓ Accepted answer

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.

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

2 comments

0

although i might be wrong

0

yes this is wrong

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