So I am making a gun previewing script. When you click the weapon it will display it in the viewport. It displays the weapons as expected but I am not able to remove the children
here is my code so far
--August 1 2020
--Test script for the viewport frame
--
--Made by:Mikey2019d
---------------
-- Constants --
---------------
local cam = game.Workspace.CurrentCamera
local Guns = game.Workspace.Weapons
local ViewPort = game.Players.LocalPlayer.PlayerGui.ScreenGui.ViewportFrame
local Buttons = game.Players.LocalPlayer.PlayerGui.ScreenGui.GunLables
local ChildrenOfViewport = ViewPort:GetChildren()
---------------------
-- Set Camera type --
---------------------
cam.CameraType = "Scriptable"
-------------------------
-- Set viewport camera --
--------------------------
ViewPort.CurrentCamera = cam
-----------------
-- Main script --
-----------------
for i,button in pairs(Buttons:GetChildren()) do
button.MouseButton1Click:Connect(function()
local gunSelected = button.Name
for i,Gun in pairs(Guns:GetChildren()) do
if Gun.Name == gunSelected then
for i = 1,#ChildrenOfViewport,1 do
ChildrenOfViewport[i]:Destroy()
ChildrenOfViewport:ClearAllChildren()
end
local focus = Gun:FindFirstChild(tostring(Gun.Name) .. "FOC").CFrame
cam.CFrame = focus
local gunClone = Gun:Clone()
gunClone.Parent = ViewPort
end
end
end)
end
Edited script:
--August 1 2020
--Test script for the viewport frame
--
--Made by:Mikey2019d
---------------
-- Constants --
---------------
local cam = game.Workspace.CurrentCamera
local Guns = game.Workspace.Weapons
local ViewPort = game.Players.LocalPlayer.PlayerGui.ScreenGui.ViewportFrame
local Buttons = game.Players.LocalPlayer.PlayerGui.ScreenGui.GunLables
local ChildrenOfViewport = ViewPort:GetChildren()
---------------------
-- Set Camera type --
---------------------
cam.CameraType = "Scriptable"
-------------------------
-- Set viewport camera --
--------------------------
ViewPort.CurrentCamera = cam
-----------------
-- Main script --
-----------------
for i,button in pairs(Buttons:GetChildren()) do
button.MouseButton1Click:Connect(function()
local gunSelected = button.Name
for i,Gun in pairs(Guns:GetChildren()) do
if Gun.Name == gunSelected then
local focus = Gun:FindFirstChild(tostring(Gun.Name) .. "FOC").CFrame
cam.CFrame = focus
local gunClone = Gun:Clone()
gunClone.Parent = ViewPort
for i = 1,#ChildrenOfViewport,1 do
if ChildrenOfViewport[i].Name ~= gunSelected then
ChildrenOfViewport[i]:Destroy()
end
end
end
end
end)
end
It is inside starterCharchtersScripts and it is a local one and viewport is parented to a frame called ScreenGui
Thanks for taking the time to help me. Thanks.

4 comments
yes
ok so the problem is that i was trying to destroy a table. Thanks for your help (=
Sorry but it does not work
YES it works Finally. Sorry i was putting the wrong variable