SubtractAsync IS nil, please help

I’m trying to make a tool that cuts thing, but the second time I cut something I get this.

Players.xXRed_VoidXx.Backpack.Katana.Cut:20: attempt to index nil with ‘Anchored’

The SubtractAsync is actually nil, I managed to print it. I can cut any part or union once, but if I cut the same thing twice it errors.
(sorry for the super scuffed katana, im not a good modeler)


The reason I went to the big block is because it had already been cut, not even by the katana, to show that it still errors.
Code:

local touch = true
script.Parent.Blade.Touched:Connect(function(part)
	if touch == true then
		touch = false
		part.Anchored = true
		local paren = part.Parent
		part.Parent = workspace.PreCuts
		local slice = game.ReplicatedStorage.Slice:Clone()
		slice:PivotTo(script.Parent.Blade.CFrame)
		slice.Parent = workspace.Slices
		local double = part:Clone()
		double.CFrame = part.CFrame
		double.Parent = workspace.PreCuts
		local leP = {part, slice.Left}
		local left = part:SubtractAsync(leP,Enum.CollisionFidelity.PreciseConvexDecomposition)
		local riP = {double, slice.Right}
		local right = double:SubtractAsync(riP,Enum.CollisionFidelity.PreciseConvexDecomposition)
		workspace.PreCuts:ClearAllChildren()
		workspace.Slices:ClearAllChildren()
		left.Anchored = false
		right.Anchored = false
		left.Velocity = part.Velocity
		right.Velocity = part.Velocity
		left.Parent = paren
		right.Parent = paren
		part:Destroy()
		wait(1)
		touch = true
	end
end)
1 Like

The union is parented to PreCuts when you do the SubtractionAsync, after that you clear all the children in PreCuts which is also deleting the union.

No, the base part and it’s clone is parented to PreCuts.

I know it’s a mess, but right under the velocity, it sets the union’s parent to paren, which for now is workspace. Also I’m not sure if you have watched the video, but you can see the error in it.

Remove part from leP table and remove double from riP table.
You are currently subtracting the part with itself so there would not be anything left, so no union is created.

Maybe this video might help

Well your advice help ALOT. Although for some reason, after a few slices, it does it again. Any ideas why?
Edit: im going to do a little more testing, I think it has to do something with the blade.

After a few slices, there might be nothing left to slice (aka the entire part got subtracted), maybe add a check to see if the union was created or not and continue from there.

So would I just do like,

if not left then
return
end

?

Yeah you could do that, make sure to check if the right one exists too.