Problem with a disappearing block

I have a problem with a disappearing block script. The script works only once but once the brick has regenerated the script doesn’t work anymore.
Here is the exact script:

local DissapearingPart = script.Parent
local debounce = true

DissapearingPart.Touched:Connect(function(hit)
local Humanoid = hit.Parent:FindFirstChildWhichIsA(“Humanoid”)
if Humanoid and debounce == true then
debounce = false
DissapearingPart.Transparency = 0.5
wait(1)
DissapearingPart.Transparency = 1
DissapearingPart.CanCollide = false
wait(3)
DissapearingPart.Transparency = 0
DissapearingPart.CanCollide = true
wait()
end
end)

P.S please don’t mind the spelling plssss :3

You are defining the first part in the start. When it is destroyed, that variable changes to nil and when a new part is spawned, the variable still is nil, therefore not affecting the new part.

so what changes do I have to make to the script?

You can add a new part with Instance.new and change the variable of the part like so:

part = newPart

It seems you forgot to reset your debounce variable at the end of the function. Since the debounce remains false, and the function checks to see if it is true, the script won’t pass the if-statement.

The solution is to set the debounce variable to true at the end of the function, like so:

local DissapearingPart = script.Parent
local debounce = true

DissapearingPart.Touched:Connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
	if Humanoid and debounce == true then
		debounce = false
		DissapearingPart.Transparency = 0.5
		wait(1)
		DissapearingPart.Transparency = 1
		DissapearingPart.CanCollide = false
		wait(3)
		DissapearingPart.Transparency = 0
		DissapearingPart.CanCollide = true
		wait()
		debounce = true -- reset to true
	end
end)
1 Like

You Forgot To Reset The Debounce.
Here is a Easier code

local DissapearingPart = script.Parent -- The Part
local Debounce = false -- Set The Debounce To False

DissapearingPart.Touched:Connect(function(hit)
    local Humanoid = hit.Parent:FindFirstChild(“Humanoid”)
    if (Humanoid) then
        if Debounce then return end -- prevent the debounce
        Decounce = true -- Set The Debounce to true
        for i = 0,1,0.05 do -- a loop to set the Transparency of the DissapearingPart smoothly
            wait(0.1) -- the time taked to loop the statement
            DissapearingPart.Transparency = i
        end
        DissapearingPart.CanCollide = false -- after the loop it will set the CanCollide to false
        wait(5) -- Wait The Amount Of Time You Want
        DissapearingPart.Transparency = 0 -- Transparency 0 after the 5 seconds
        DissapearingPart.CanCollide = true -- CanCollide set to true
        Decounce = false -- and finally Decounce set to false
    end
end)

Hope It Helped.