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

Can I use return to call a set of numbers?

Asked by 10 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
        else
            return -151.4, 36.5, -138
        end     


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

        plr:LoadCharacter()
        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
3
Answered by 10 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]
0
This is new to me, thanks for the help! :D AwsomeSpongebob 350 — 10y
Ad
Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 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 Vector3.new(-151.4, 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;
end

local A,B,C = numbers();
local vec = Vector3.new( numbers() );

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

Answer this question