Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How do i remove all the children of viewport?

Asked by 4 days ago
Edited 4 days ago

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

Log in to vote
1
Answered by 4 days ago

: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()

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

Answer this question