What does returning do?

Hello, I have no Idea what returning is and if someone could explain it that would be great! If you could explain what the code would do without returning and with returning that would be amazing also. :smiley:

game.Players.PlayerAdded:Connect(function(plr) -- new player joins, plr is the player
    if plr.Name == "VIVIDC0RE" then -- self explanatory
        return -- if the statement above this is true, then return it, and what retrning does is it kinda ends the script there
    end
    print("Hello") -- if you were running this as VIVIDC0RE, it wouldnt print because the script was already returned, basically ended right at that line
end)

its kinda hard to explain but

works in loops and functions by the way

That’s what I thought it did, thanks for the validation!

return will return any data. Here is an example:

local math_equation = 1+1

function performMathEquation(equation, answer)
    if equation == answer then
        return true
    end
end

if performMathEquation(math_equation, 2) == true then
   print("This will print when performMathEquation returns the value of true")
end

I believe if you do it inside of a if statement, function etc. it will completely halt it.

1 Like

You’ll get better, faster, and more detailed answers by searching it on Google or somewhere similar. Googling things is a skill you need to know if you want to be a programmer.

The return statement, a necessary skill for all programmers. Let’s say you have a function:

function foo()
return "Hello"
end

and another function:

function bar()
return "World"
end

You can run these functions to get the values of them:

local Hello = foo()
local World = bar()

Optionally, you could print them out:

print(Hello.." "..World)
1 Like