Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Script meant to destroy a part inside the player only works once?

Asked by 3 years ago
Edited 3 years ago

hi! I'm a little rusty with scripting since I haven't done it in a while. In a game I'm working on, I have a button that adds a part inside the player called "Character." I also have a button that deletes this part when its not being used. (For context, the player is only supposed to have this brick attached to them in a specific area, and then it's removed once they leave that area.)

This is the script:

debounce = false
function onTouch(hit)

    if game.Players:FindFirstChild(hit.Parent.Name) ~= nil and debounce == false then

        local player = game.Players:FindFirstChild(hit.Parent.Name)
        local c = player.Character:GetChildren()
        local character = player.Character:WaitForChild("Character")

        debounce = true

        character:Destroy()



        for name, child in pairs(player.Character:GetDescendants()) do 
            if child:IsA("Part") or child:IsA("MeshPart") or child:IsA ("UnionOperation") or child:IsA("Decal") then
                child.Transparency = 0 
            end
        end



    end
end

script.Parent.Touched:connect(onTouch)

I'm guessing the problem comes somewhere in this part of the script:

        local character = player.Character:WaitForChild("Character")

        debounce = true

        character:Destroy()

It works the first time perfectly, but if I touch the first button (which adds the part back into my character) and then back on this button that's supposed to destroy it, it doesn't work the second time around. Does anyone know why that could be? There's no error in the output.

1 answer

Log in to vote
0
Answered by 3 years ago

debounce is true after the function onTouch(hit) loop somewhere create a line to set it to false

0
oh that's what it was! thank you! yoshikins101 11 — 3y
0
yw Mister33j 86 — 3y
Ad

Answer this question