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

Can anyone help me with a multival function?

Asked by 5 years ago

So i am working on a function that will make it so i can have the same function for different variables. Here's what I tried:

function Multival (func, amt)
    local ValsToReturn
    for i = 1, amt, 1 do
        ValsToReturn = ValsToReturn, func
    end
    return ValsToReturn
end

func is the function you need to call, amt is the amount of times you need to repeat it. the func returns something.

0
I think this idea is fundamentally flawed. Just use the standard loops and create a separate function for everything. A function should have 1 and only 1 purpose. Think about it this way: Your function is going to loop through other functions. Thats basically useless, if you've already created a function, just loop it separately. Forgive me if im forgetting something or missing something here. Psudar 882 — 5y
0
I should have said that i was trying to use it as: local val1, val2, val3, val4 = Multival (functionTest(), 4) GuiHelperSince2014 70 — 5y
0
Oh I gotcha! Ill write a solution now. Psudar 882 — 5y

1 answer

Log in to vote
0
Answered by
Psudar 882 Moderation Voter
5 years ago

So im guessing you want to return multiple values from a function into more than one variable, at least thats what I hope.

Its actually super simple once you figure out that you can write variables on the same line like this:

local variable1, variable2 = 1, 2

So, if you want a function that returns more than one value to more than one variable, you can do this:

local function returnSomething()
    return 1, 2, 3, 4
end

local var1, var 2, var3, var4 = returnSomething()

print(var1)
print(var3)

Expected Output: 1, 3

Hopefully that helps some.

0
Also I should mention that I believe it pretty much always assigns the variables in whatever order you returned them in Psudar 882 — 5y
0
@Psudar only problem with that is the functions im using func() to represent a function that returns an GUI element like a frame. GuiHelperSince2014 70 — 5y
Ad

Answer this question