Table.remove not working?

Hello, hope you are having a great day.

For some reason, after running table.remove and printing the table, it doesn’t seem to actually remove the value. I have tried using table.find after removing, and it gives nil, so it should be removed.

Here’s the script:

game.ReplicatedStorage.events.delete.OnServerEvent:Connect(function(plr, tableToDelete)
	local data = HttpService:JSONDecode(save:GetAsync("User-".. plr.UserId))
	
	local merged = {}
	
	if data then
		for i, v in data do
			table.insert(merged, v)
		end
	end
	
	for i, v in plr.marshmallowsCollected:GetChildren() do
		if not table.find(merged, v) then
			table.insert(merged, v)
		end
	end
	
	print(merged)
	
	for i, v in tableToDelete do
		if table.find(merged, v) then
			table.remove(merged, table.find(merged, v))
			plr.marshmallowsCollected:FindFirstChild(v):Destroy()
		end
	end
	
	print(merged)
	
	save:SetAsync("User-" .. plr.UserId, HttpService:JSONEncode(merged))
end)
3 Likes

are the elements on both list ordered the same?

I don’t think the order here matters, because even if it was incorrect order table.remove should still work.

1 Like
		if table.find(merged, v) then
			table.remove(merged, table.find(merged, v))
			plr.marshmallowsCollected:FindFirstChild(v):Destroy()
		end

is this if statement actually triggering? Try adding a print statement inside it.

I did, I added a print that will say “found” if it runs, and it printed.

I modified your script slightly for testing purposes, and it seems to work perfectly for me.

Screenshot_1
Screenshot_2

So there’s not a problem with your code, at least not in the bit I snipped, must be something else.

I’m guessing there’s something wrong with your table “tableToDelete”, have you tried taking a look?

I have printed the tableToDelete, and it is exactly what I want. I’m going to test something and let you know what happens.

Somehow it suddenly started working? I have no idea what is going on.

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