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

Help with function variables?

I am trying to make a function that weighs items in your table (not literal weight, but math weight) I am currently stuck with finding out how to make it so that you can add as many values to the variable part of my function.

function tableweigh(table, num)

end

--the function is supposed to work like this

lol = {1,3,1,3,5,6}
tableweigh(lol,1,3,12,3,1,3)

--or this 

wut = {1,2,3}
tableweigh(wut,2,3,1)

Note: Please only help me with the variable part, this is a project I want to do mostly on my own.

9 comments

0

does the table have the value?

0

Why would anyone answer. You'll just delete your question afterwards /:

0

I only delete my questions because I don't like getting alot of notifications

0

Smell that? It's the stench of a liar. Whatever I don't really care. I'm just not going to be helping you. Good luck.

0

Wow... sorry I deleted them okay?

0

You can manage your notifications in the setting btw. You can disable notifications from comments and such.

0

Ok, I will check it out

0

Sorry if I sounded mad. But you can understand when someone answers your question and then the question just gets deleted afterwards. It takes away the entire point of answering questions. But sorry. I hope we can remain... Not enemys.

0

Boy am I glad to hear that :)

1 answer

✓ Accepted answer

You can do this with variadic functions and the ... variable.

HOWEVER, in this case, I think one of the following alternatives would work better:

function weigh(values, weights)
	for i = 1, #values do
		local vi, wi = values[i], weights[i]
	end
end

weigh( {1, 2, 3}, {5, 3, 7} )

function weight(data)
	for i = 1, #data do
		local vi, wi = data[i].value, data[i].weight
	end
end

weigh{ {value=1, weight=5}, {data=2, weight=3}, {data=3, weight=7} }

If you know you aren't going to be repeating values, then a dictionary would be even better:

function weigh(data)
	for vi, wi in pairs(data) do
	end
end

weigh {
	[1] = 5,
	[2] = 3,
	[3] = 7,
}

Variadic Functions and Tuples

Here is a good description of tuples.

Most of the time when you use , in Lua, that can be treated as a tuple. It's sort of like a list without a table holding it together.

If you use the special name ... as on of the parameters, then it's a tuple that holds all of the (remaining) parameters.

The easiest way to process it is to just dump it into a table (which is another reason why you normally just want to pass a list as the argument)

function weight(values, ...)
	local weights = {...} -- dump input into a table
	for i = 1, #values do
		print(values[i], weights[i])
	end
end

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