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

What is "return" and "floor"?

Asked by
Cr0ws 20
10 years ago

[1]I see return end in scripts a lot but never understood it

Example:

c =  workspace.Part

if c== nil then  return end

[2]What is the purpose of floors in lua?

2 answers

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

return is a Lua keyword that "returns" to the place in the code where the current function was called. Nothing can come after it in the current code block:

function func2()
    print("end")
    return
end

function func1()
    func2()
    print("func2 done first")
end

func1()
print("this prints last")

The Lua intepreter (what runs your code in-game) automatically puts a return before any closing end in a function that doesn't have a return statement there already, as at the end of func1 in the above code.

If you understand the terminology, return pops the current function off the call stack, as opposed to calling a function to push it to the call stack.

People use return inside of if statements to cancel the function call if some parameter is not met. For example, if I had a script that I only wanted to run when a Player causes the Touched event to fire:

local sp = script.Parent

sp.Touched:connect(function(hit)
    if not hit then return end --hit sometimes doesn't exist at all!
    if not game.Players:GetPlayerFromCharacter(hit.Parent) then return end --GetPlayerFromCharacter returns `nil` if `hit.Parent` is not a player's Character.
    print("Some player Touched me!")
end)

This is done so that parameter checking can be done without causing a ridiculous amount of nesting. The code in the block below is identical in functionality to the block above, but you can see the difference in how it looks:

local sp = script.Parent

sp.Touched:connect(function(hit)
    if hit then
        if game.Players:GetPlayerFromCharacter(hit.Parent) then
            print("Some player Touched me!")
        end
    end
end

This can be difficult to debug at times, and can get incredibly annoying to maintain as you add more checks, which is why the return method is preferred.

floor is a function from the Lua math library that rounds whatever number passed to it as an argument down to the nearest integer. There is also a math.ceil (ceiling) function that rounds up to the nearest integer.

local num = 25.2326
print(num) -- 25.2326, about (may be off due to floating point error
print(math.floor(num)) --25

local num2 = 8.9 --Note that you would typically round this *up* to 9
print(math.floor(num2)) --8, since floor always rounds down.

A quick hack in Lua, since all numbers are dynamically typed (that is, you can convert between integers and floating point numbers instristically), is to add .5 to a number before passing it to math.floor, automatically rounding it to the nearest integer, up or down.

Ad
Log in to vote
-1
Answered by 10 years ago

I was going to type the answers but you changed the page immediately.

math.floor is used to round up the numbers like math.floor(3.65) = 4 and math.floor is mostly used to change velocity from non int to int and return end just ends the whole script like it has breezed over every code but made no iterations to the code fx if i say return end and put some blah blah blah in the bottom, The code gets Skipped.

Answer this question