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

How do i make the parts stop deleting if barricade1remove is not touched anymore?

Asked by 8 years ago

I'm trying to make a barricade to where if barricade1remove is being touched then the BarA-BarE remove in a time span of 3 seconds and when barricade1remove is not being touched anymore, then the parts stop removing. I know I need a TouchEnded event somewhere but I don't know where.

CODE:

local partFind = game.Workspace
game.Workspace.barricade1remove.Touched:connect(function(hit)
if hit.Parent:FindFirstChild('Humanoid') then
wait(3)
partFind.BarA:Remove()
wait(3)
partFind.BarB:Remove()
wait(3)
partFind.BarC:Remove()
wait(3)
partFind.BarD:Remove()
wait(3)
partFind.BarE:Remove()
partFind.Wall:Remove()
end
end)

1 answer

Log in to vote
0
Answered by
Link150 1355 Badge of Merit Moderation Voter
8 years ago
local part = script.Parent
local cframe = part.CFrame

-- Keeps track of whether the player is currently touching the brick.
local touching = false


local function onTouched(hit)
    local character = hit.Parent

    if game.Players:GetPlayerFromCharacter(character) then
        touching = true -- A player started touching the brick.
    end
end

local function onTouchEnded(hit)
    local character = hit.Parent

    if game.Players:GetPlayerFromCharacter(character) then
        touching = false -- A player stopped touching the brick.
    end
end


part.Touched:connect(onTouched)
part.TouchEnded:connect(onTouchEnded)


local parts = {}

-- Every 0.1 second, check if the brick is touched.
while true do
    -- Execute code as long as the player is touching the brick
    -- and there are parts to delete.
    while touching and #parts > 0 do
        -- Remove the last part from the list then delete it.
        local partToDelete = table.remove(partToDelete)

        partToDelete:Destroy()
        -- Note how I use "Destroy()" instead of "Remove()". 
        -- This is because remove is a deprecated method. You shouldn't use it anymore.
        wait(0.01)
    end

    wait(0.1)
end

Hope this helps. Please make sure to upvote and accept my answer if it did! If you need anything else, let me know.

Ad

Answer this question