Connection is established but nothing happens after?

local debounce = false
script.Parent.ActivatingRegion.Transparency = 0

local function UnleashThing(Part)
	print("Touch")
	print(Part.AssemblyLinearVelocity.Magnitude * script.Parent.ActivatingRegion.Size.Magnitude)
	if game.Players:GetPlayerFromCharacter(Part.Parent) and Part.AssemblyLinearVelocity.Magnitude * script.Parent.ActivatingRegion.Size.Magnitude > 1500 then
		if debounce == false then
			debounce = true
			workspace.Music.Scream:Play()
			Part.Parent.MovementScript.SpeedVal.Value = 4
			script.Parent.ActivatingRegion.Object.Value:Fire(game.Players:GetPlayerFromCharacter(Part.Parent))
			wait(8)
			debounce = false
		end
	end
end

local function RespawnDoor(Region)
	repeat wait() until game.Players:FindFirstChildWhichIsA("Player")
	game.Players:FindFirstChildWhichIsA("Player").CharacterAdded:Wait()
	local c = Region:Clone()
	c.Transparency = 0
	c.Parent = script.Parent
	c.Touched:Connect(UnleashThing)
	c.AncestryChanged:Connect(RespawnDoor)
end

script.Parent.ActivatingRegion.Touched:Connect(UnleashThing)

script.Parent.ActivatingRegion.AncestryChanged:Connect(RespawnDoor)

The part gets destroyed after being touched (not on this script though), so i made it so when it’s Parent property was changed to nil, it would make a new part, and it does, the problem is that the connections don’t seem to work, as the function that would trigger when the part is touched doesn’t happen.

I think maybe it is because deleting the part removes it before the function can connect?
Maybe you should move it to server storage and replace it then delete it so the function has time to work?

Just tried that, no luck, maybe it’s something else? :sweat:

1 Like

The official roblox wiki says that you can use it to detect deleted objects.
https://developer.roblox.com/en-us/api-reference/event/Instance/AncestryChanged

Maybe try something like this

local function RespawnDoor(child, parent)
	repeat wait() until game.Players:FindFirstChildWhichIsA("Player")
	game.Players:FindFirstChildWhichIsA("Player").CharacterAdded:Wait()
        if not parent then
	        local c = child:Clone()
	        c.Transparency = 0
	        c.Parent = script.Parent
	        c.Touched:Connect(UnleashThing)
	        c.AncestryChanged:Connect(RespawnDoor)
        end
end

script.Parent.ActivatingRegion.AncestryChanged:Connect(RespawnDoor)

Also what is this line for?

c.AncestryChanged:Connect(RespawnDoor)

so when the new object is deleted it respawns another one.

Yeah but isnt that what this does?

script.Parent.ActivatingRegion.AncestryChanged:Connect(RespawnDoor)

yeah but that only applies to the current part, not the next one to be spawned