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

How do I use table.remove() correctly if this doesn't work...?

for i,v in pairs(mytable)do
	if type(v) ~= "string" then
		table.remove(mytable,i)
	end
end
table.sort(mytable)

Error: attempt to compare string with userdata

Someone please explain -_-?

2 answers

✓ Accepted answer

You're shifting the table, but iterating forwards. This has the awkward side-effect of causing the iteration to skip some keys.

The solution? Iterate backwards through the table.

for i=#mytable, 1, -1 do
	local v = mytable[i];
    if type(v) ~= "string" then
        table.remove(mytable,i)
    end
end
table.sort(mytable)

This will avoid skipping iterations, and it will mean that the internal comparison from table.sort won't whine about comparing anything other than strings.

You havent put anything in the table

4 comments

0

First define the table

0

If there wasn't anything in the table it wouldn't tell me it attempted to compare something with something if there was nothing.

0

v is the things in the table but theres nothing in v and u trying to compare to something

0

try this: myTable = [1, 2 , 4]

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