Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i remove all the children of viewport?

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.

1 answer

✓ Accepted answer

:GetChildren() returns a table of children. Since it's a table, you can't do :ClearAllChildren on it (it'd look like {"index"}:ClearAllChildren() which wont work). I believe you want to do ViewPort:ClearAllChildren()

4 comments

0

yes

0

ok so the problem is that i was trying to destroy a table. Thanks for your help (=

0

Sorry but it does not work

0

YES it works Finally. Sorry i was putting the wrong variable

This discussion is preserved as it was. New answers, comments, and votes are closed.