Why is this reference variable changing?

I’m making a modulescript for a leaderboard system and, for some reason, a reference variable changes when its reference changes. I have no clue how this is happening because I’m not changing it myself, and it should stay the way it was set to before since I experimented with this segment of code in a different script and it worked as intended, yet it changes no matter what. How can I fix this?

        prev = LB 
	print(prev) --prints the version of the LB I want
	
	local function ClearOut(LB, Scores)
		local clear = {}
		for spot, key in ipairs(LB) do
			if Scores[key].mathplace == 0 then
				table.insert(clear, key)
			end
		end
		for i,v in ipairs(clear) do
			local pos = table.find(LB, v)
			table.remove(LB, pos)
		end
	end
	
	ClearOut(LB, Scores) 
		
	print(prev) --prints the updated LB from ClearOut function, not what I want

what’s reference

But, once you remove something from table (Array I think), everything in table will change it index
(This breaks for looping I think, kind of)

so like
1, 2, 3, 4, 5. you remove 3
it becomes 1, 2, 4, 5

I’m sure you know this

This is not what I meant. I meant that I have two variables, and I have set one of those variables to the other. Normally, the variable that has been set to the other does not change when the variable that it is set to changes, it should stay the same. However, in this example, that is not happening, and that’s what I want to fix.

1 Like

Is this similar to something I had maybe

I was setting a couple tables in one table, and when I change one of them, every other one changes too

That sounds similar to my problem. The variable in question is changing when the table I have set the variable to changes, which of course isn’t what I want.

I’m not sure myself why this is happening, because it wasn’t before for me either

Use table.clone

After testing, it seems that table.clone is the proper method of doing what I needed! Thanks for your help

1 Like

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