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

Is this how you use the return function?

Asked by 7 years ago

For example:

thing = false

function okay()
wait(2)
if thing == false then
print ("Hello World!")
thing = true
return okay()
elseif thing == true then
print("Goodbye!")
end
end

If it works correctly, it should print hello world, and then goodbye.

0
after 2 seconds. MustangHeart 67 — 7y

3 answers

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

return "returns" a value of a function. For example:

local function returnOne()
    return 1
end

The function above will return 1. Example:

print(returnOne()) --> 1

print(returnOne()+10)-->11

However functions are useful because you can give them arguments, and you can return different things based on the arguments.

local function evenNumberCheck(num)
    return num%2==0
end
%, or modulus, gives the remainder.

The above function will check if numbers are even, and return a bool based on that. Example:

print(evenNumberCheck(1)) --> false

print(evenNumberCheck(6)) --> true

if(evenNumberCheck(8*2))then
    print("8*2 makes an even number")
else
    print("8*2 doesn't make an even number")
end

For more info, I suggest reading this answer to the same question.

Ad
Log in to vote
2
Answered by
farrizbb 465 Moderation Voter
7 years ago

You use return to get the value of a function and to stop it

Log in to vote
0
Answered by 7 years ago

You would use return to get the value of a function.

local a = true
local b = true

function ReturnValues()
return a and b
end


function PrintValues()
print(tostring(ReturnValues())) --tostring converts things to strings
end

PrintValues()
0
so basically, does it go back to the start of the script? (or as specified?) MustangHeart 67 — 7y
0
it'll go back to where it was called.  Azarth 3141 — 7y

Answer this question