Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Can anyone help me with a multival function?

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.

3 comments

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.

0

I should have said that i was trying to use it as: local val1, val2, val3, val4 = Multival (functionTest(), 4)

0

Oh I gotcha! Ill write a solution now.

1 answer

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.

2 comments

0

Also I should mention that I believe it pretty much always assigns the variables in whatever order you returned them in

0

@Psudar only problem with that is the functions im using func() to represent a function that returns an GUI element like a frame.

This discussion is preserved as it was. New answers, comments, and votes are closed.