How To Do a server cleanup every x Amount Of Time?

Someone suggested I use a server cleanup every x minutes for my game because I have btools in my game, how do I do the script?

Sorry if I’m misunderstanding, the question is kind of vague. What exactly are you “cleaning up”? All the parts created/cloned with btools every few minutes? The pseudocode would look something along the lines of this:

local TIME = 60 * 5 -- every 5 minutes

while task.wait(TIME) do
    -- clean up... no idea what this means
    for _, part in pairs(workspace:GetDescendants()) do
        -- some if statement so you don't just delete everything
        if part:IsA("Part") and part:FindFirstChild("CreatedByBtools") then -- ex
            part:Destroy() -- if you wanted to delete them
        end
    end
end

This would only work assuming a ValueBase (or some other instance really, just named “CreatedByBtools”) is inserted into the part when interacted with while using Btools.

Hey, im from the posters previous post. The poster wants to reset this game (BTOOLS FUN! - Roblox) every x amount of time. For example, if a house was destroyed in 5 minutes it would be re-built along side everything else in that server.

LocalScript or Just a Normal Script?

Normal script, as its Server-Sided.

In ServerScriptService? Or Just Workspace?

Im not to sure, im assuming ServerScriptService.

As @Y0utube_Dino said, it’s a regular script preferably under ServerScriptService.

So resurrecting everything that was deleted? For this to work you’d have to store a copy (or just get the information) of every part deleted under the workspace and store it in an array. Then, after X minutes, loop through that array and place new parts back in workspace with their same information.

local deletedParts = {}

workspace.DescendantRemoving:Connect(function(descendant)
    if descendant:IsA("BasePart") and (not descendant.Parent:FindFirstChild("Humanoid") then
        deletedParts[descendant.Name] = descendant:Clone()
    end
end

while task.wait(TIME) do
    for partName, partClone in pairs(deletedParts) do
        partClone.Parent = workspace
    end
    table.clear(deletedParts) -- reset this array
end

This code above is just pseudocode and may not function exactly as intended, it is not tested

1 Like

I just thought about this, don’t know why I overthought it, you might just be talking about “refreshing the server.” Like, everything that was originally in the server will just be re-inserted so all the old parts get deleted. I don’t know why I didn’t think this might’ve been what OP wanted.

If you went this route, it’d probably be easier to just chuck all of the stuff you want to keep under a folder and call it like “Map”

image

Something like that containing all the important stuff. With that, you could just do:

local ServerStorage = game:GetService("ServerStorage")
local mapFolder = workspace:WaitForChild("Map"):Clone()
mapFolder.Parent = ServerStorage

local TIME = 60 * 5

while task.wait(TIME) do
    if workspace:FindFirstChild("Map") then
        workspace["Map"]:Destroy()
    end
    mapFolder:Clone().Parent = workspace
end

This is also untested

1 Like

Gonna give this a shot now.
(30 word limit)

Can confirm, this does work. Also added a print so you know when its been reset, when you are testing @PowerArmor2020 look in the console for “Server Cleaning DONE” if it does not return that in the console, then you have messed something up. Make sure you put all your map assets in a folder in workspace.

Like this:
image

Here is the updated script:

local ServerStorage = game:GetService("ServerStorage")
local mapFolder = workspace:WaitForChild("Map"):Clone()
mapFolder.Parent = ServerStorage

local TIME = 60 * 5
print("Server Has Been Reset!")

while task.wait(TIME) do
	if workspace:FindFirstChild("Map") then
		workspace["Map"]:Destroy()
	end
	mapFolder:Clone().Parent = workspace
end