luarobloxluaulua-5.1

How to delete clones - Roblox Studio?


I want to build a lag game and I want that when you click a button, all the clones of the part will be gone but when I script it, it's not working. This code is for cloning the part (this is a normal script in a part):

while true do
    wait()
    local clonepart = script.Parent:Clone()
    clonepart.Parent = game.Workspace
    clonepart.Anchored = false
end

This is the code for deleting (a local script in a Text button GUI):

local part = game.Workspace.cloner

script.Parent.MouseButton1Up:Connect(function()
    part:Destroy()
end)

It is removing the part which is making the clones.


Solution

  • The easiest way to remove everything with one line would be to put all of the clones into a Folder in the Workspace. Then when you click the button, you fire a RemoteEvent to tell the server to simply destroy the Folder and/or all of its children.

    So try something like this :

    1. Have your spawner block create a Folder for all of its clones in the Workspace called LagFolder
    2. Create a RemoteEvent in ReplicatedStorage called DestroyLagFolder
    3. Have the LocalScript that listens to your your mouse button events fire the RemoteEvent.
    local DestroyLagFolderEvent : RemoteEvent = game.ReplicatedStorage.DestroyLagFolder
    
    script.Parent.MouseButton1Up:Connect(function()
        -- tell the server to destroy all the spawned clones
        DestroyLagFolderEvent:FireServer()
    end)
    
    1. Have the part spawner Script listen for when the RemoteEvent event gets fired.
    local PART_SPAWN_TIME = 1 --second
    
    -- create a place for all of the clones to go
    local LagFolder = Instance.new("Folder")
    LagFolder.Parent = game.Workspace
    
    -- listen for when the event fires to clean up all of the clones and the spawner
    local DestroyLagFolderEvent : RemoteEvent = game.ReplicatedStorage.DestroyLagFolder
    DestroyLagFolderEvent.OnServerEvent:Connect(function(player)
        LagFolder:Destroy()
        script.Parent:Destroy()
    end)
    
    -- start creating clones
    while wait(PART_SPAWN_TIME) do
        local clonepart = script.Parent:Clone()
        clonepart.Parent = LagFolder
        clonepart.Anchored = false
    end