Rapid UnionAsync on Touched not working

so basically im making a snow script where literally everything that touches it leaves an imprint via part:SubtractAsync() buuuuut it for some reason isnt working, I read the api reference and still dont know what im doing wrong, even when i hook up a print command like this: print(hit.ClassName) on the Touched() function and it always printed Part. Please help.

script:

local part = script.Parent.SnowPart

part.Touched:Connect(function(hit)
	local newUnion = part:SubtractAsync({hit})
	part:Destroy()
	newUnion.Parent = script.Parent
end)

sno

1 Like

From what I can see, there are 2 problems.

  1. The part variable isn’t being set again. So once it get destroys, there’s nothing to call it anymore.
  2. There’s nothing connecting the newUnion to have a touch function that’ll allow it to be repeatable.
1 Like

ok, just edited the script to be:

local part = script.Parent.SnowPart

part.Touched:Connect(function(hit)
	local newUnion = part:SubtractAsync({hit})
	part:Destroy()
	newUnion.Parent = script.Parent
	part = newUnion
end)

but it still has the same exact error. speaking of i forgot to put this in the OP but this is the error.

Right, that’ll happen if a mesh-part, union, or anything else that touches it. Also your solution still won’t work since it wouldn’t repeat. The variable being set is good, but the function was attached to that specific Instance, thus it needs to be set again for the new one. It’d also be wise to put a debounce in there so the game won’t lag out horribly from trying to subtract from a Union 100 times in a second. Here’s how it should look:

local Part = script.Parent.Brick --or whatever part you're getting
local Debounce = false

local OnTouch OnTouch = function(Hit)
	local Allowed = Hit:IsA("Part") or Hit:IsA("WedgePart") or Hit:IsA("CornerWedgePart") or false
		if not Allowed or Debounce then return end
	Debounce = true
	local newUnion = Part:SubtractAsync({Hit})
	Part:Destroy()
	Part = newUnion
	Part.Parent = script.Parent
	Part.Touched:Connect(OnTouch)
	delay(1,function() Debounce = false end)
end

Part.Touched:Connect(OnTouch)

Tested it in Studio and seems to work just fine.
Maybe there’s a more efficient way to set this up, but if it works it works.

i tried that and go the same exact error (at line 8 to be precipice)
Could you send me the rblx file your using?

That’s the exact script I’m using, so it wouldn’t be any different due to a rblx file.
Just what exactly are you forcing the part to touch that’s causing the error?

everything… its supposed to make everything that touches it indent into the snow

Well it plainly can’t work with everything. Only certain parts are capable of doing this, as per the error states, Only blocks, spheres, cylinders, wedges and corner-wedges are currently supported.

ok, ill dull it down and only make it indent when touched by a character’s legs.

If you still end up having trouble, I’ve attatched a place file here so you can see for yourself:
Test.rbxl (18.6 KB)

No need cause it works now! Final script is here for anyone else having similar problems. Thanks ShadukoSan!

local Part = script.Parent.SnowPart --or whatever part you're getting

local Debounce = false

local OnTouch OnTouch = function(Hit)

if Hit.Name ~= "Left Leg" and Hit.Name ~= "Right Leg" then return end

local Allowed = Hit:IsA("Part") or Hit:IsA("WedgePart") or Hit:IsA("CornerWedgePart") or false

if not Allowed or Debounce then return end

Debounce = true

local newUnion = Part:SubtractAsync({Hit})

Part:Destroy()

Part = newUnion

Part.Parent = script.Parent

Part.Touched:Connect(OnTouch)

delay(0,function() Debounce = false end)

end

Part.Touched:Connect(OnTouch)
2 Likes