Returning in Scipting

  1. What do you want to achieve? I want to know what returning is in scripting.

  2. What is the issue? I watch tutorials and they never seem to hel

  3. What solutions have you tried so far? Youtube, Developer Hub

Here is a script i recently seen that had me even more curious

local chosenPiggy = players[RandomObj:NextInterger(1,#players)]

return chosenPiggy

Now please help. I cant every seem to understand what it means or does, and how did you learn it?

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

2 Likes

In simple terms, returning is basically something that you retrieve within 1 function

In the code you have, if you return chosenPiggy, you can retrieve its results if you manage to call it from outside the function

local function ChooseRandomOinky()
    local chosenPiggy = players[RandomObj:NextInterger(1,#players)]

    return chosenPiggy --This would be either nil, or the Random Player you want to choose from
end

local ConfirmPlayer = ChooseRandomOinky() --This is how we obtain a returned value, by calling the function

if ConfirmPlayer then --We're checking if this isn't equal to nil
    --Now do your stuff here
end
6 Likes

So what I’m getting from this is when I return something and call the function somewhere else its returning whatever i returned making me able to use the same line again right?

Pretty much

This

Is basically the same as this

You’re just returning it inside your function so that you can use it for later within your script if you ever so desire to call it again

1 Like

Example


function random()

local part = game.workspace.Part
return part

end

local colors = part

if colors then

colors.BrickColor = color3.fromRGB(“255,255,255”)

end

So would this work? (30characte)

to expand on this bit, if we tried

function One()
    function Two()
        return 3
    end
    
    Two()
end

print(One())

that would print nil, because it is per function
but we can return the function to make it work

function One()
    local function Two()
        return 3
    end
    
    return Two()
end

print(One())

this would print 3 because we returned the function that was equal to three

2 Likes

Close, you would reference the function that returns the object.

function random()
  local part = game.workspace.Part
  return part
end

local colors = random()

if colors then
  colors.BrickColor = color3.fromRGB(255,255,255)
end
1 Like

Wait im confused how did you get 3 to work withiut calling it somewhere else?

Let’s not overcomplicate the issue at hand here

@TinyFlair No, because part would be equal to nil in this instance cause it’s defined as a local variable inside the function, and you’re not calling the random() function that the Part is returning back

1 Like

my bad, just thought it was helpful information

1 Like

Oh i see what i did wrong thank you

Didnt notice that thank you so much

1 Like

Let’s look at this a visual way.
We’ll use print as an example here.

function getSentence(name) --Grab our input (the name we want to display)
  return "Hello " .. name .. "! How's it hanging?" --Return a new string with our text and name involved
end

print(getSentence("Jimmy")) --Expected output: "Hello Jimmy! How's it hanging?"
print(getSentence("TinyFlair")) --Expected output: "Hello TinyFlair! How's it hanging?"
1 Like

isn’t it game.Workspace with a capital W

It is, I just copy pasted his code and sent it back. Didn’t change anything.

And game.workspace is deprecated, but still works for some older games that used it before it was changed.

There’s also just the variable “workspace” that you could use instead.

oh I didn’t even know that was a thing

What does it mean to return nothing?

You would just be simply ending the function early

Now that you’ve got a basic & general concept of how returning works, let’s expand it a bit more :thinking:

Say I have this function

local Bool = false

local function CheckBool()
    if Bool == nil then
        return --This is basically returning nothing
    end

    if Bool == true then
        return "This Bool is true!"
    elseif Bool == false then
        return "This Bool is false!"
    end

    print("This will never print because we're actually returning/ending our function at least once")
end

print(CheckBool())

Now, we got a little bit more things here, but let’s go ahead and break this down here

First, we’ll start with the nothing return

This would check if the Bool variable hasn’t been assigned to anything, and if it is then we can end our function early to prevent it from continuing the script! Usually you want to return if you don’t want to continue the function

Next, is our Bool check returns

Simple stuff, but this will check if our Bool variable is true/false, if it’s 1 of the 2 then we can return back a string confirming what Value our Bool is!

Lastly, we print the result of the returned value and it will either return 3 things:

  • nil - If the Bool variable hasn’t been assigned/defined by anything
  • false - If the Bool variable is equal to false (Which would be the False String)
  • true - If the Bool variable is equal to true (Which would be the True String)
1 Like

When you do
local someinstance = Instance.new(instance)
This function returns you the instance and that is the reason you are able to store it in the variable.
So imagine this function

> local function Add(a,b)
>` return a + b` 
> end 
> local result = Add(3,3)
> print(result) --> 6

This function is returning you the sum of the arguments provided, which you can then store into a variable.

1 Like