Log in to vote

Can I use return to call a set of numbers?

Asked by 7 years ago

Is it possible to use return to call a set of numbers? I'm sorry if this doesn't make much sense, I'm still kind of new to this and I'm not really sure how to word it right. Here is a section of my script so you can get a better idea of what I'm asking.

    if game:GetService("MarketplaceService"):PlayerOwnsAsset(plr,passId) then
        Val = DataStore:GetAsync("Pos")
        if Pos then
            return Pos
            return -151.4, 36.5, -138

            Hum = Character:FindFirstChild("HumanoidRootPart")
            HumPos = Hum.Position
            local Humanoid = Character:FindFirstChild("Humanoid") -- find the humanoid, and detect when it dies
            if Humanoid then
                    wait(respawnTime) -- delay, then respawn the character

        local HumPos = Val

On line 6 I am returning a set of numbers, is this possible or do I need to use a different approach? Thanks!

2 answers

Log in to vote
Answered by 7 years ago

Returning the numbers as a table would be one approach. Change line 6 to:

return {-151.4, 36.5, -138}

Then you can call each number with its corresponding table index, such as:

local myTable =  {-151.4, 36.5, -138}
local number1 = myTable[1]
local number2 = myTable[2]
local number3 = myTable[3]
This is new to me, thanks for the help! :D AwsomeSpongebob 350 — 7y
Log in to vote
Answered by
BlueTaslem 18054 Moderation Voter Administrator Community Moderator Super Administrator
7 years ago

JetCrusherTorpedo is correct, usually you would want to return a list (table) of values.

In this case, it would be better to return a Vector3 since this encapsulate 3 numbers into one value, and is actually the way we represent positions:

return, 36.5, -138);

However, you can do exactly what you did. You can return multiple values from a function. Those will be treated as consecutive arguments to a function it is applied to, or as several variables in an assignment:

function numbers()
    return 1, 2, 3;

local A,B,C = numbers();
local vec = numbers() );

print(A); -- 1
print(B); -- 2
print(C); -- 3
print(vec); -- 1, 2, 3
This too is really helpful, thanks! :D AwsomeSpongebob 350 — 7y
One question, what are the semicolons for? What do they do? AwsomeSpongebob 350 — 7y
0 It's (usually) just a stylistic choice. I always use them. Almost all of the time you never need to use them. BlueTaslem 18054 — 7y

Answer this question