Why won't my instance get removed from a table

I have a table full of bullet instances, every time the player shoots a bullet it adds an instance to the table of that bullet.

table.insert(BulletList,Instance_Create)

Instance_Create = the bullet.
and I use this code to remove it from the table after I delete the bullet

local TableNum = table.find(BulletList,Instance_Create)----This is how i locate the bullet in the table
delay(6,function()
			table.remove(BulletList,TableNum)
		end)

But when I do this it prints weird numbers,-----Have a print() somewhere else in the script
Sometimes it removes all of them sometimes it doesn’t.

It adds to the table whenever the player shoots
image
And Removes them from the table when they touch the ground, or after 6 seconds have past
image
Yet it prints weird number like this after all bullets have hit the ground or after 6 seconds
▼ {
[1] = Bullet,
[2] = Bullet
}

1 Like

Shouldn’t you use table.find() when you’re about to remove it?

delay(6,function()
	local TableNum = table.find(BulletList,Instance_Create)----This is how i locate the bullet in the table
	table.remove(BulletList,TableNum)
end)
1 Like

I would like it if you show the entire script so I can help you properly.

1 Like

.-.

local Player = game.Players.LocalPlayer
local Char = ((Player.Character and Player.Character.Parent) and Player.Character) or Player.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")
local HumanoidRoot = Char:WaitForChild("HumanoidRootPart")
---Services
local Rep = game:GetService("ReplicatedStorage")
local RepModels = Rep:WaitForChild("Models")
local RepEvents = Rep:WaitForChild("RemoteEvents")
local TweenService = game:GetService("TweenService")
local CAS = game:GetService("ContextActionService")
local CollectionService = game:GetService("CollectionService")
local Debris = game:GetService("Debris")
local Mouse = Player:GetMouse()
---Module
local ToolModule = require(game.ReplicatedStorage.ModuleScript)
----
local PlayerConfig = Char:WaitForChild("Player_Configuration")

local Weapon_Config = script:WaitForChild("Configuration")
local UniConFig = script.Parent.UniversualTool_Configuration
local StateAnims = UniConFig:WaitForChild("StateAnimations")
local CombatAnims = UniConFig:WaitForChild("CombatRanged_Animations")

local Muzzle = script.Parent:WaitForChild("Muzzle")

local Accuracy = Weapon_Config.Accuracy
local Damage = Weapon_Config.Damage
local MagSize = Weapon_Config.MagSize
local BulletSpeed = Weapon_Config.Bullet.BulletSpeed
local BulletTime = Weapon_Config.Bullet.BulletTime

local BulCount = 1
--
local LoadingAction = PlayerConfig:WaitForChild("DoingAction")
---Bullet
local DefultBullet = RepModels:WaitForChild("Bullet")

local BulletList = {

}

local BlackListTouch = {
	Char,
	script.Parent,
	BulletList

}


----Functions

function DebrisV2(Instance_Tag, DelayTime, CommandType, Table_Tag)
	delay(DelayTime, function()
		if CommandType == "Destroy" then
			Instance_Tag:Destroy()
		elseif CommandType == "Table Destroy" then
			table.remove(Table_Tag, Instance_Tag)
		end
	end)
end


function doActionStun(ActionTime)
	LoadingAction.Value = true
	delay(ActionTime,function()
		LoadingAction.Value = false
	end)
end

function LocalBulletCreate(BulletModel, parent, Pos, Dist, Speed, Accuracy, dmg)
	if BulletModel ~= nil then	
		local CanHit = true

		local Instance_Create = BulletModel:Clone()
		CollectionService:AddTag(Instance_Create,"Bullet")
		table.insert(BulletList,Instance_Create)
		local TableNum = table.find(BulletList,Instance_Create)

		Instance_Create.Parent = parent
		Instance_Create.Position = Pos

		Instance_Create.CFrame = CFrame.lookAt(Pos,Dist) * Accuracy

		Instance_Create.Anchored = false
		TweenService:Create(Instance_Create, TweenInfo.new(0.25,Enum.EasingStyle.Linear,Enum.EasingDirection.In), {Velocity = Instance_Create.CFrame.lookVector * Speed}):Play()
		Debris:AddItem(Instance_Create,6)
		delay(6,function()
			table.remove(BulletList,TableNum)
		end)


		Instance_Create.Touched:Connect(function(hit)
			if CanHit == true then

				if hit and not table.find(BlackListTouch,hit.Parent) and not table.find(BulletList, hit) then		
					if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
						local humanoidH12 = hit.Parent:FindFirstChild("Humanoid")
						RepEvents:FindFirstChild("DamageAdd"):FireServer(humanoidH12, dmg)
						Instance_Create:Destroy()
						table.remove(BulletList,TableNum)
					else
						Instance_Create:Destroy()
						table.remove(BulletList,TableNum)
					end
				end
			end
		end)
		return Instance_Create
	end
end


--Combat


function Fire(actionName, inputState, inputObject)
	if LoadingAction.Value == false and LoadingAction.HardLock.Value == false then
		if inputState == Enum.UserInputState.Begin then
			doActionStun(0.5)
			local Orientation = CFrame.new(HumanoidRoot.Position, Mouse.Hit.Position)
			local x, y, z = Orientation:ToEulerAnglesYXZ()
			local BulletToRootRot = CFrame.Angles(x, y, z) 

			Humanoid:LoadAnimation(CombatAnims:WaitForChild("Fire_Weapon")):Play()
			HumanoidRoot.Orientation = Vector3.new(HumanoidRoot.Orientation.X, y, HumanoidRoot.Orientation.Z)
			
			task.wait(0.05)
			for count = 1, BulCount do
				local Ran1 = math.random(-Accuracy.Value,Accuracy.Value)/100
				local Ran2 = math.random(-Accuracy.Value,Accuracy.Value)/100
				local Ran3 = math.random(-Accuracy.Value,Accuracy.Value)/100

				LocalBulletCreate(DefultBullet, workspace, Muzzle.Position, Mouse.Hit.Position, BulletSpeed.Value, CFrame.Angles(math.rad(Ran1),math.rad(Ran2),math.rad(Ran3)), Damage.Value)	
				--	RepEvents:FindFirstChild("Bullet"):FireServer(DefultBullet, workspace, Muzzle.Position, Mouse.Hit.Position, BulletSpeed.Value, CFrame.Angles(math.rad(Ran1),math.rad(Ran2),math.rad(Ran3)), Damage.Value)	
			end
		end
	end
end

---Connections

CAS:BindAction("FireWeapon", Fire, true, Enum.UserInputType.MouseButton1)

Thanks. I‘ll look into it now.

1 Like

How would that change the outcome? the Numbers in the table aren’t moving just getting higher

Because of how tables work, when one element is removed, all indices after that element’s index shift by -1. Meaning if you remove index 1 from this table:{2, 5, 6, 9, 7}, The table will look like this: {5, 6, 9, 7}. This means that by the time you remove it, your variable TableNum points to the wrong index.

1 Like

Hugh, I’ll test it and see what happens.

Yeah that makes sense. I cannot spot any other things.

1 Like

Seemed to fix everything, Thanks for the help!

1 Like