Only can print table, cant print everything inside table

hi im ctg i got issule on table
im making matchmaking system but i got werid thing
when warn the table it return table
but when warn everything inside table it said the table is string

for i, v in pairs(GlobalMatchmaking) do
	warn(v) -- this warn table
	table.foreach(v, warn) -- this return error
	if #GlobalMatchmaking[i] + #GlobalMatchmaking[i] == 4 then	
		table.remove(GlobalMatchmaking[i], table.find(GlobalMatchmaking, i))
		--replicateStorage.MatchmakingClient:FireClient(v, "Matched", convertToHMS(times))	
		break
	end
end

error: invalid argument #1 to ‘foreach’ (table expected, got string)

Theres a value inside GlobalMatchmaking table which is a string instead of a table.
You are iterating a table and each entry contains supposedly a table, if any of those entries its not a table you will get that error, like this:

local Tab = {}
Tab["a"] = {"stuff"}
Tab["b"] = {"stuff2"}
Tab["c"] = "the string" -- issue

for i, v in pairs(Tab) do
	warn(v) -- this warn table
	table.foreach(v, print) -- this return error
end

You could patch it by checking if its a table before to do any table functions:

local Tab = {}
Tab["a"] = {"table1"}
Tab["b"] = {"table2"}
Tab["c"] = "a hidden string"
Tab["d"] = 3

for i, v in pairs(Tab) do
	if typeof(v) == "table" then
		table.foreach(v, print) -- this is a table
	elseif typeof(v) == "string" then
		warn("string:", v)
	else
		warn("what else?", typeof(v), v)
	end
end

thanks! it worked rn, ima learn more abt table

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