Error in talbes

Hey, I was working on a game script and ran into this error
attempt to modify a readonly table
can someone help me fix this

Script
MatchSystem.MapVotes = {[1] = {}, [2] = {}, [3] = {}}
for i, v in pairs(MatchSystem.MapVotes) do
	table:clear(v)
end

MatchSystem is a module

the script just works fine if I clear each tables manually like in the script below

Script
MatchSystem.MapVotes = {[1] = {}, [2] = {}, [3] = {}}
table.clear(MatchSystem.MapVotes[1])
table.clear(MatchSystem.MapVotes[2])
table.clear(MatchSystem.MapVotes[3])

there’s no error with this script and it works fine, but why is script not ok with for loop ?

You used : rather than . for table:clear(v)

3 Likes

still the same error and it’s fine to use both : or . for table.clear()

They are different. The colon syntax will pass table as the first argument, but you want to pass v so you should write table.clear(v)

2 Likes

Can I ask why do you think so? x:y(z) is a sugar syntax for x.y(x, z).

Yes it is fine to use table:clear() if you want to clear the content of table but I don’t see why would you and table is read-only anyway.

1 Like

nah, any of them should work for this case ig, I am not sure about it

and even I am not sure why the table is read only, I am confused with this

Do the understand the difference between : and . in Lua? Your post doesn’t seem to suggest so.

You said the error is the same even when you switched from table:clear(v) to table.clear(v) am I right? Then the problem is that v is read-only but it doesn’t seem like you actually tried using table.clear(v) so I can’t say for certain?

1 Like

I tried running the script using both : and . but got the same error for both the cases

@Blockzez is correct. If you try using table:clear(v) in studio it will underline v in studio with the error

Argument count mismatch. Function ‘table.clear’ expects 1 argument, but 2 are specified

Once it’s changed to table.clear(v) the error is gone.
I tested your code and I got the same error with table:clear but it runs fine with table.clear so not sure why it’s still producing the same error for you.

1 Like

let me recheck

umm yep now it runs with . and not with :, I guess I gotta look up about usage of : and . once again

1 Like

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