Table value doesn't change

I know that it may sound weird but this values doesn’t want to change like at all

local rarities = {rare = true, epic = false, legendary = false}

for i, v in rarities do
	if v == true then
		print(v)
		v = false
		print(v)
	end
end

print(rarities)

Output:

like the game does detect some value being true in it but at the same time when I check the same table outside the for loop all the values are still the same. I’m sorry if the solution is super easy but I actually don’t know what is the cause of this

In your loop you have the variable v. This value is a Boolean, and in Luau when it is assigned in this manner it is copied - that is to say modifying it will not modify the original. If you want to modify the original you need to set the table value, not your local copy.

rarities[i] = false
1 Like

It worked, thank you! But I have a question why was this made this way?

1 Like

Because i and v are variables, and variables are meant to act as temporary storages of values. This is the how variables are treated for most general purpose programming languages (i.e. Python, C, Rust, Zig), and it’s more practical than what you might think.

you are changing the variable instead of table
Its not a pointer aswell

You can technically avoid this sort of behavior by using buffers as since they are treated like pointers/tables

Mhm, I would know. But let us not introduce such concepts to a feedback post from someone who’s only beginning, as this might nerdsnipe them — and since they aren’t prepared to tackle such concept — might cause them further confusion.

1 Like

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