How do I check multiple things in a table at once?

What am I trying to do?

Currently, I’m trying to make a math.random script that doesn’t repeat any number it’s already generated.

Script

local TableDemo = {}

local Table2 = {1,2,3,4,5,6,7,8,9,10}

while true do

	local Num = math.random(1,10)
	
	print(Num)
	
	
	if table.find(TableDemo, Num) then

		wait()
		
		print('NotAdded')
		
		
		if TableDemo == Table2 then --Issue
			
			print('Cleared')
			
			table.clear(TableDemo)

		end
		

	elseif not table.find(TableDemo, Num) then

		table.insert(TableDemo, Num)
		
		print('Added'..Num)
		
		wait(0.5)

	end


end

Issues

The main issue is just my lack of knowledge on these things.
Where I put --Issue is where the issue is.

Everything else works, but the main issue is just the checking of if TableDemo is equal to Table2.
All the script does is loop print numbers that weren’t added (numbers already in the list) when the list is done.

My question

Is there any way to check TableDemo's values and to see if it’s 1-10 so I can reset it?
All I need is an answer above and the solution for this issue.

What I think is wrong

If you know what’s wrong feel free to not read this. Or correct me if you want.

I think I’m getting issues because the numbers are inputted randomly into TableDemo and Table2 is organized.
(E.g. TableDemo probably looks something like {3,4,9,8,2,10,1,7,5,6} and Table2 is just set 1-10 according to value.)

Solutions I’ve tried

I’ve looked around on DevForum and the ways to do this look complicated to me and hard to understand.
I’m just trying to create something I can personally understand.
I could probably create a way to check TableDemo by going number by number, but I’m looking for a solution that’s less line intensive.

Try this?

TableDemo = {}
Table = {1,2,3,4,5,6,7,8,9,10}

while task.wait(.5) do
	local Number = math.random(1, #Table)
	
	if not table.find(TableDemo, Number) then -- if Item not found
		table.insert(TableDemo, Number)
	else
		Number = math.random(1, #Table) -- Probably useless
	end

	if #TableDemo == #Table then -- If number of items is equal to table
		table.clear(TableDemo)
	end

	table.sort(TableDemo, function(x,y) -- Sorts the Table
		return x < y -- Format to Sort the Table
	end)
print(TableDemo)
end

A better way might be this however:

TableDemo = {}
Table = {1,2,3,4,5,6,7,8,9,10}


function TableFunction(t: {}, ct: {})
	local Number = math.random(1, #ct)

	if not table.find(t, Number) then -- if Item not found
		table.insert(t, Number)
	else
		return
	end

	if #t == #ct then -- If number of items is equal to table
		table.clear(TableDemo)
	end

	table.sort(t, function(x,y) -- Sorts the Table
		return x < y -- Format to Sort the Table
	end)
	print(t)
end

while task.wait(.5) do TableFunction(TableDemo, Table) end
1 Like

It works, thank you!

I feel really bad because you wrote all of that and I think put a ton of thought into it but the solution to what I was going for just required me changing
if TableDemo == Table2 then
to
if #TableDemo == #Table2 then.

I really appreciate the help and teaching me that you can put a # in front of a table variable to check the amount of things inside of it. I did not know that until now.

1 Like

Its Fine, at least i helped you find a solution :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.