I’m trying to understand more about returning by watching this video by CovertCode but I don’t understand how and why he returned true and false back to the VerifyPlayer(hit). Does return true or return false determine whether the function will happen or not?
I was only watching a video and didn’t write any code but my main question is what return true or return false does to the “VerifyPlayer(hit)”
Think of a function like a box containing code. When you call a function, you open the box and run the code. The code inside does some stuff, and usually creates a result. The result is what you take out of the box. That’s what “returning” does. You’re taking the result out of the box (the result can sometimes be nothing too).
In this case it sounds like for VerifyPlayer, the return value means whether or not the player is verified.
In the case local someVariable = VerifyPlayer(...), you can imagine the VerifyPlayer() call being replaced in code entirely by the value it returns after it runs. (e.g. false or true), so in the instant after that function runs, the code looks like local someVariable = true.
In the context of the video, the purpose of the function is to check whether or not the Player exists. If it exists, the function will return true because the following conditions have been fulfilled.
If the conditions are not fulfilled, the function won’t return true. That leaves it to return false. The value that is returned acts as the result of the function, like @PeZsmistic said.
This means that the function has a value, which is why he calls the function within a condition.
Essentially, you can replace VerifyPlayer(Hit) with either true or false because it equals the result of the function, which is defined by the return statement.
Pretty close.
More like, VerifyPlayer is being turned into a value, and depending on the value being true or false, something will happen or won’t happen.
E.g. In this if statement, if it returns true, then “something” happens, otherwise, “something” does not happen, and the whole block between the if ... then end is skipped.
if VerifyPlayer(...) then
-- something happens
end
To expand, if you want something to happen if VerifyPlayer is not true, you can invert the value by using “not” (i.e. not VerifyPlayer()), or you can use an else: if ... then ... else .... end, where the first part happens if the value is true, or the second part happens otherwise.