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

Is there a way for tables to check if there is more of one thing in it?

So I am currently trying to make a script that checks if there is more of one thing in the table. For example:

t = {"Orange", "Purple", "Grey", "Purple"}

The table would know that there is 2 of Purple and 1 of Orange and Grey.

2 answers

✓ Accepted answer

I would do this:

local function getNum(array, value)
	local num = 0
	
	for i = 1, #array do
		if array[i] == value then
			num = num + 1
		end
	end
	
	return num
end

t = {"Orange", "Purple", "Grey", "Purple"}

print(getNum(t, "Purple"))

The function takes an array, and a specified value.

Set up a variable to keep track of the number of occurrences of the value. Iterate through the array, and for each time we stumble across an element that matches the value we're looking for, add 1 to the variable we set up before iterating through the array. After the loop finishes, return the variable.

add this to your code:

if #t > 0 then
	print("More things are in this table")
end

2 comments

0

They're asking for a way of determining if an element shows up more than once in a table, not if the table has multiple elements.

0

Oh then you need to sort

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