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

Why do I need the return in this function?

Asked by
Master_JJ 229 Moderation Voter
7 years ago

I saw this small function when rescripting the pistol by UristMcSparks. I was wondering why the 'return event' was needed, on line 7.

local function createEvent(eventName)
    local event = game.ReplicatedStorage:FindFirstChild(eventName)
    if not event then
        event = Instance.new("RemoteEvent", game.ReplicatedStorage)
        event.Name = eventName
    end
    return event
end

3 answers

Log in to vote
2
Answered by 7 years ago

Return

returns works the same way in coding as it does in real life. If a company sends something to you, but you don't feel satisfied with it, you return it.

In programming, the return statement is used as a final output of something. Once you've used a return statement, the scope your code is in ends there for the time being. This is why if you were to say something like this:

return nil
print("Hello world")

You'd get this error:

Error in script: '<eof>' expected near 'something'

Why is it important?

For reasons you may or may not already imagine, returning something for the right reason is very important. This can give code and input / output mechanic opposed to just providing input and getting nothing back. This is most typically represented through the use of functions.


For example, we could make a function that divides a number you input by 2, and return that value. But where does it get returned to? Let's take a look:

local function divide(n)
    return n / 2
end

print( divide(10) ) -- This will print 5, since the function returns 5 (10/2)

This should answer our question of where data gets returned to, which is really something you'd quite expect. It just returns the value to where ever it was called. We can also take advantage of this by saving return values to variables, like this:

local function divide(n)
    return n / 2
end

local result = divide(10) -- Now "result" is 5
print(result) -- Prints 5

Hope this helped, if you have any questions, just let me know.

Ad
Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

Explanation

What is happening in the script is the function is given the value eventName. eventName is a RemoteEvent that should be in ReplicatedStorage. The script will set a variable "event" equal to the value of the event object in ReplicatedStorage.

The function will then put the event through a if then statement which the condition will either be the object or nil for true or false. If the condition is "not false", like the negative of false being true, then the function will create a new RemoteEvent object.

The function will then return the name of the new event object and will name it the eventName provided by the calling line.

    local function createEvent(eventName)
        local event = game.ReplicatedStorage:FindFirstChild(eventName) --The script will search ReplicatedStorage for the object with the name "eventName"
        if not event then --If the object exists then the negative of true would be false, thus skipping this if then statement as it will only follow true values. If the object does not exist then it be nil or false, the negative of false being true so the if then statement should run.
            event = Instance.new("RemoteEvent", game.ReplicatedStorage) --A new RemoteEvent will be created.
            event.Name = eventName --The name for the RemoteEvent will be what was provided to the function when called. I.e. createEvent("MyRE")
        end --Ends the if then statement.
        return event --Returns the event object, whether it was originally found or made in the if then statement.
    end

It's more of keeping track of what event names are usable as you do not want 50 RemoteEvents named "Hello" or something like that. It will find an already existing event otherwise return a new event object.


Hopefully this answered your question. If it did, do not forget to hit the accept answer button. If you have any questions, feel free to comment below and I will get back to you.
Log in to vote
0
Answered by
einsteinK 145
7 years ago
-- With the return
print(createEvent"test") --> test (which is a RemoteEvent)
-- Without
print(createEvent"test") -->    (nothing gets returned)

Answer this question