Error when using UnionAsync

Hello, I am trying to make a system that creates a hole out of a part you touch.

However, I am getting the error message: “Unable to cast value to Objects”

How do I fix this?

Parthole is a negative part that I am cloning

code:

game.ReplicatedStorage.Hole.OnServerEvent:Connect(function (player, mousePos)
	local character = player.Character
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local raycastResults = workspace:Raycast(character.Head.CFrame.p, (mousePos - character.Head.CFrame.p)*1000, raycastParams)
	local clone = workspace.PartHole:Clone()
	clone.Parent = workspace
	local basepart = raycastResults.Instance
	basepart:UnionAsync(clone)
end)

Had this problem before. Script union operations require a table as input, so using a single object as input won’t work. Just wrap it in {}, and it should work: basepart:UnionAsync({clone}). This also lets you subtract or add multiple parts in a union with one operation, by storing the parts you want in a table variable and then running the function.

1 Like

The error is gone but its not unioning it

It is unioning it! Union scripting is weird. The old parts don’t get deleted on union like they do in studio, since what you’re really doing is making a new part. You have to delete them manually in the script. Also, you have to parent the union to something, just like creating a new part (because it is one)!

2 Likes

Ok so this worked, and that’s why I’m marking this as the solution. However I am getting this weird glitch
where the negative part is transformed into a put and doesn’t make a hole until you click another area

video:Screen Recording 2021-07-01 At 10.01.58 PM GIF | Gfycat

code:

game.ReplicatedStorage.Hole.OnServerEvent:Connect(function (player, mousePos)
	local character = player.Character
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local raycastResults = workspace:Raycast(character.Head.CFrame.p, (mousePos - character.Head.CFrame.p)*1000, raycastParams)
	local clone = workspace.PartHole:Clone()
	clone.Parent = workspace
	local basepart = raycastResults.Instance
	clone.Position =raycastResults.Position
	local newpart = basepart:UnionAsync({clone})
	newpart.Parent = workspace
	basepart:Destroy()
	clone:Destroy()
end)

It looks like your negative part isn’t anchored, so that could be causing this. Also, using SubtractAsync() and a regular part might help: it’s the only method I’ve used and I havent run into any non-deletion issues. A way to get around it, and I think the one I used is to just make the negative part transparent so it doesnt show lol

The duplication glitch is something I tried to fix myself when working with unions, and happens when you click on a part that isn’t completely finished unioning yet, which means that two unions get created from the old part instead of one. This problem (which is suprisingly hard to deal with by queueing and not just debouncing anything that tries to double union) is actually one of the main reasons why I ditched live unioning for my game, along with the long response times for even slightly complex unions.

1 Like

The subtract async method worked great for me! Surprising how buggy unions are through scripting. Thanks for the help!