Ad
Log in to vote
0

How to detroy an instance?

Asked by 9 days ago
Edited 9 days ago

Ive tried to create a lag test game for roblox but im struggling to destroy once its been created, ive tried everything but it still wont work

Heres the code:

local function SpawnParts()
    wait()
    local LagPart1 = Instance.new("Part")
    LagPart1.BrickColor = BrickColor.new("Institutional white")
    LagPart1.Material = ("ForceField")
    LagPart1.Parent = game.Workspace
    LagPart1.Position = Vector3.new(12, 61.5, 5)
    LagPart1.Name = "LagParts"
    LagPart1.Size = Vector3.new(6,6,6)
end

Reset.MouseButton1Up:Connect(function()
    Reset1 = true
    workspace:WaitForChild("LagPart1"):Destroy()
end)

2 answers

Log in to vote
0
Answered by 9 days ago
Edited 9 days ago

To destroy all lag parts which you've created (assuming they're all called "LagParts"), try the following:

Reset.MouseButton1Up:Connect(function()
    for i, v in pairs(game.Workspace:GetDescendants()) do
        if v:IsA("BasePart") and v.Name == "LagParts" then
            v:Destroy()
end)

The code loops through the entire workspace and checks for any parts named "LagParts". v:IsA("BasePart") checks the type of object, so it will only get destroyed if it's a physical part.

Ad
Log in to vote
1
Answered by
eLunate 5136 Trusted Moderation Voter Community Moderator
9 days ago

You're naming the part LagParts instead of LagPart1

Additionally, using WaitForChild will only select one part. You can instead group all of the created Parts into a model and using Destroy on that model instead (and then regenerating it)

Answer this question