How can I destroy a instance stored in a table?

Hi, I’m making a game and I want to be able to store 2 seperate instances in 2 seperate tables for each player that fires the even shown below. However, after the player fires sequence 2, it will attempt to destroy the instance in the table and then set it to nil, but it doesn’t work. How can I make it work?

local t = {}
local t2 = {}

event.OnServerEvent:Connect(function(player, sequence)
	if not table.find(t, player) then
		t[player] = ServerStorage.Bari:WaitForChild("BariCylinder"):Clone()
		t2[player] = Instance.new("ForceField", player.Character.HumanoidRootPart)
	end
		if sequence == 1 then
			t[player].Parent = player.Character
			t2[player].Visible = false
		elseif  sequence == 2 then
			local DissapearGoal = {}
			DissapearGoal.Transparency = 0
			local TWInfo = TweenInfo.new(0.2,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)
			local tween = TweenService:Create(t[player], TWInfo, DissapearGoal)
			print(t[player])
		tween:Play()
		tween.Completed:Wait()
		t[player]:Destroy()
		t[player] = nil
		t2[player]:Destroy()
end)

The only solution i’ve tried currently is debris service, not sure if it does much difference but it dosn’t work either.

edit: the tween doesn’t play either, but somehow the sciprt makes it past the tween.Completed:Wait()

edit 2: Okay, after a lot of debugging using print, i found out that whenever the event was being fired, at the part here:

if not table.find(t, player) then
	t[player] = ServerStorage.Bari:WaitForChild("BariCylinder"):Clone()
	t2[player] = Instance.new("ForceField", player.Character.HumanoidRootPart)
end

gets fired when i fire it the second time as well. Apparently, whenever I fire it, it can’t find the said t, player and it is nil. Why? If i fix this im sure it will work.

Im not sure if this will work but have you tried creating a variable of the instance from the table then calling :Destroy()?

local obj = t[player]
obj:Destroy()

I just tried, and it dosn’t work

Could you show us the errors in the output? (if it shows an error)

It dosnt. It prints everything it shows in the code as well

Is this part of a larger code? I see that you don’t have an end for

if sequence == 1 then
   --your code stuff
elseif sequence == 2 then
   -- more of your code
  <--should there be an end here?
end)

you dont need a end for after elseif if there is a if :confused:
elseif and if is one

That was hard to read… But you do need a end after elseif.

event.OnServerEvent:Connect(function()
   if not  findtable then

   end
   if x == 1 then

   elseif x == 2 then

   end

end)

this is the full code

BariShieldevent.OnServerEvent:Connect(function(player, key, sequence)
	if not table.find(t, player) then
		t[player] = ServerStorage.Bari:WaitForChild("BariCylinder"):Clone()
		t2[player] = Instance.new("ForceField", player.Character.HumanoidRootPart)
	end
		if key == "wuihd781hdjkshnd892hdgbu71ihf891uihdg1" and sequence == 1 then
			t[player].Parent = player.Character
			t2[player].Visible = false
	elseif key == "wuihd781hdjkshnd892hdgbu71ihf891uihdg1" and sequence == 2 then
		local instance1 = t[player]
			local DissapearGoal = {}
			DissapearGoal.Transparency = 1
			local TWInfo = TweenInfo.new(0.2,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)
			local tween = TweenService:Create(instance1, TWInfo, DissapearGoal)
			print(t[player])
		tween:Play()
		tween.Completed:Wait()
		t[player]:Destroy()
		t[player] = nil
		t2[player]:Destroy()
		t2[player] = nil
		print("Set nil")
	elseif key ~= "wuihd781hdjkshnd892hdgbu71ihf891uihdg1" then
		local UserId = player.UserId
		local success, errorMsg = pcall(function()
			banDataStore:SetAsync(UserId, true)
		end)
		if success then
			print("Banned a noob exploiter")
		end
		player:Kick("Nice try, noob exploiter")
	else
		print(player,key,sequence)
	end
end)

I ended up finding the solution myself after 3 days with this being the finished code:

local t = {}
local t2 = {}

BariShieldevent.OnServerEvent:Connect(function(player, key, sequence, pos)
	local playerUserId = player.UserId
	if not t[playerUserId] and not t2[playerUserId] then
		print(t[playerUserId])
		t[playerUserId] = ServerStorage.Bari:WaitForChild("BariCylinder"):Clone()
		t2[playerUserId] = Instance.new("ForceField", player.Character.HumanoidRootPart)
		Debris:AddItem(t[playerUserId], 10)
		Debris:AddItem(t2[playerUserId], 10)
	end 
	if key == "wuihd781hdjkshnd892hdgbu71ihf891uihdg1" and sequence == 1 then
		t[playerUserId].Parent = player.Character
		local bariShieldsoundClone = game.Workspace.Sounds.Bari.bariShield:Clone()
		bariShieldsoundClone.Parent = player.Character.HumanoidRootPart
		t2[playerUserId].Parent = player.Character
		t[playerUserId].Position = Vector3.new(pos.X, pos.Y+3.2, pos.Z)
		t2[playerUserId].Visible = false
		bariShieldsoundClone:Destroy()
	elseif sequence == 2 then
		local DissapearGoal = {}
		DissapearGoal.Transparency = 1
		local TWInfo = TweenInfo.new(0.2,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)
		local tween = TweenService:Create(t[playerUserId], TWInfo, DissapearGoal)
		tween:Play()
		wait(0.2)
		Debris:AddItem(t[playerUserId], 0)
		Debris:AddItem(t2[playerUserId],0)
		wait(0.5)
		t[playerUserId] = nil
		t2[playerUserId] = nil
	elseif key ~= "wuihd781hdjkshnd892hdgbu71ihf891uihdg1" then
		local UserId = player.UserId
		local success, errorMsg = pcall(function()
			banDataStore:SetAsync(UserId, true)
		end)
		if success then 
			print("Banned a noob exploiter")
		end
		player:Kick("Nice try, noob exploiter")
	end
end)

not much difference, but apparently i souldn’t use table.find in a if statement.