Hello! For a game I’m making, I’m making it so when you enter a portal, it clears the workspace so I can add another “dimension” by cloning from ServerStorage. How can I clear everything in the workspace when I touch a part, EXCEPT the player(s)? If anyone can answer this, thank you!
for i, v in workspace:GetChildren() do
if not game.Players:GetPlayerFromCharacter(v) then pcall(v.Destroy, v) end
end
probably
Parent the world under any instance and move it elsewhere.
That the only solution that will damage as less perfomance as possible.
Here’s my solution that allows you to keep certain classes (use their ClassName as a string in the classesToKeep table), and certain instances (put the path to them in partsToKeep), assuming they are not hidden in a folder or model.
local Players = game:GetService("Players")
local classesToKeep = {
"Terrain",
"Camera"
} :: { string }
local partsToKeep = {
workspace.Baseplate
} :: { Instance }
local function destroyWorkspace(): ()
for _, instance: Instance in ipairs(workspace:GetChildren()) do
local className = instance.ClassName
if table.find(classesToKeep, className) then
continue
end
if table.find(partsToKeep, instance) then
continue
end
if Players:GetPlayerFromCharacter(instance) then
continue
end
instance:Destroy()
end
end
Just parent the world under a folder or a model (depending on cases and depding if steaming is enabled or not)
If I didn’t want the baseplate kept, could I remove the partsToKeep segments?
Yes, you can remove or add anything you want to the two tables. The code may error if you completely remove the tables though; leave them empty if you want to skip over those checks.
I added the code to my portal when touched, it doesn’t seem to work. Let me try something else real quick.
are you calling the destroyworkspace function?
No. I’m not destroying the workspace model, I want to destroy everything INSIDE workspace, from models, sounds, parts, folders, everything.
That is what the function in my code titled destroyWorkspace() does. If you copy and paste my current code into your code, it will not run. The function is stored but never called, so whenever you want to run the function just put destroyWorkspace()
Oh. My apologies, I’m a significantly inexperienced scripter so I’m just trying my best that I know.
I tried to activate the script via the touch script instead of directly putting it in said touch script, and that doesn’t work either. I removed the partstokeep stuff and this is what i have:
local Players = game:GetService("Players")
local classesToKeep = {
"Terrain",
"Camera"
} :: { string }
local function destroyWorkspace(): ()
for _, instance: Instance in ipairs(workspace:GetChildren()) do
local className = instance.ClassName
if table.find(classesToKeep, className) then
continue
end
if Players:GetPlayerFromCharacter(instance) then
continue
end
instance:Destroy()
end
end
Am I doing something wrong?
Can you show the full code that encases the code snippet you attached?
yeah but are u ever calling the destroyWorkspace() function
I already mentioned that I’m really inexperienced, I only know more basic stuff like tweens, some functions, etc. I’m trying to comprehend what tables are right now.
You mean like the code that activates this code? If that’s what you’re talking about, here it is:
local ts = game:GetService("TweenService")
for i, v in pairs(game.Players:GetPlayers()) do
v.Character:WaitForChild("Humanoid").WalkSpeed = 0
game.Lighting.ColorCorrection2.Enabled = true
wait(1)
script.Parent.Parent.Loudie:Play()
ts:Create(
game.Lighting.ColorCorrection2,
TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut),
{ Brightness = 1 }
):Play()
wait(1)
--workspace:FindFirstChild("Wind"):Destroy()
v.CameraMode = "LockFirstPerson"
local part = Instance.new("Part", workspace)
part.Name = "TP"
part.Position = Vector3.new(0, 0, 0)
part.Anchored = true
part.Size = Vector3.new(1,1,1)
part.Transparency = 1
part.CanCollide = false
task.wait()
v.Character:WaitForChild("Torso").CFrame = part.CFrame
task.wait()
v.Character:WaitForChild("Humanoid").WalkSpeed = 10
v.Character:WaitForChild("Humanoid").JumpPower = 0
v.Character:WaitForChild("Humanoid").JumpHeight = 0
part:Destroy()
game.Lighting.ColorCorrection2.Brightness = -1
game.Lighting.Ambient = Color3.new(1, 1, 1)
game.Lighting.Brightness = 1.27
game.Lighting.ColorShift_Top = Color3.new(1, 1, 1)
game.Lighting.ColorShift_Bottom = Color3.new(1, 1, 1)
game.Lighting.GlobalShadows = false
game.Lighting.OutdoorAmbient = Color3.new(1, 1, 1)
game.Lighting.ShadowSoftness = 1
game.Lighting.TimeOfDay = "12:00:00"
game.Lighting.ExposureCompensation = "0.25"
game.Lighting.Sky:Destroy()
game.Lighting.Bloom:Destroy()
game.Lighting.Atmosphere:Destroy()
wait(1)
game.SoundService.AmbientReverb = "SewerPipe"
game.Lighting.ColorCorrection2.Enabled = false
game.Lighting.starting.Enabled = true
local stargate = game.ServerStorage["Sound Storage"].Ambientser:Clone()
stargate.Parent = workspace
stargate.Playing = true
local Players = game:GetService("Players")
local classesToKeep = {
"Terrain",
"Camera"
} :: { string }
local partsToKeep = {
workspace.Baseplate
} :: { Instance }
local function destroyWorkspace(): ()
for _, instance: Instance in ipairs(workspace:GetChildren()) do
local className = instance.ClassName
if table.find(classesToKeep, className) then
continue
end
if table.find(partsToKeep, instance) then
continue
end
if Players:GetPlayerFromCharacter(instance) then
continue
end
instance:Destroy()
end
end
--workspace:FindFirstChild("Border"):Destroy()
end
The code snippet that I added is above this text.
yeah, ill take a look at this and see if i can find whats going on. Thanks
call the function by doing
destroyWorkspace()
after the function definition
By the way, the code seen here is enabled by a parenting Humanoid touched script, so it’s being enabled in order to work.