I have a script which im using to spawn Collectables in the game im working on, but for some reason the part is getting removed from the table even though every time it should be getting removed from the table it should also get deleted.
function Collectables:Spawn(Position)
local CurrencyPart = self.Object:Clone()
CurrencyPart.Parent = CollectableFolder
CurrencyPart.Anchored = false
CurrencyPart.CFrame = CFrame.new(Position)
table.insert(self.Parts,CurrencyPart)
local BP = Instance.new('BodyPosition',CurrencyPart)
BP.Name = 'BP'
BP.MaxForce = Vector3.zero
BP.P = 100
BP.D = 10
delay(15,function()
CurrencyPart:Destroy()
table.remove(self.Parts,table.find(self.Parts,CurrencyPart))
end)
end
function Collectables:ConnectHeart()
self.Heart = self.Heart and self.Heart:Disconnect() or RunService.Heartbeat:Connect(function()
for _,Player in pairs(game:GetService('Players'):GetPlayers()) do
local CharacterPosition = Player.Character and Player.Character:FindFirstChild('HumanoidRootPart').Position
for _,Part in pairs(self.Parts) do
if Player.Character and (CharacterPosition - Part.Position).Magnitude <= 2 then
Part:Destroy()
table.remove(self.Parts,table.find(self.Parts,Part))
Sounds.PlaySound('rbxassetid://17295477114',.5,CharacterPosition)
end
end
end
end)
end