Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why is getchildren index nil?

Asked by 4 years ago
Edited 4 years ago

I tried to make a camera with viewport frame in surface gui but it didn't work, It just spam errors

The script is localscript, Map is a model inside the viewport frame, The script is located on the viewport frame

Script:

local Map = script.Parent:WaitForChild("Map")

game:GetService("RunService").RenderStepped:Connect(function()
    for i, v in pairs(Map:GetChildren()) do
        v:Destroy()
    end

    for i, v in pairs(game.Workspace:GetChildren()) do
        if v.ClassName ~= "Camera" and v.ClassName ~= "Terrain" then
            v:Clone().Parent = Map
        end
    end
end)

Please help.

0
Where is the SurfaceGui located? Spjureeedd 385 — 4y
0
StarterGui FixRobloxz 61 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

The reason why your script is erroring is that you're trying to set the parent of EVERYTHING in the workspace, including the player's models, and you can't parent those to a viewport frame unless you make a clone of them.

In order to fix this, you can also make you if statement checks if there is no Humanoid inside of v. If you also want to have the player inside the viewport frame you can make the player's model Archivable property set to true. Setting the Archivable property to true allows the model to be cloned thus allowing you to parent it to the Map.

local Map = script.Parent:WaitForChild("Map")

game:GetService("RunService").RenderStepped:Connect(function()
    for i, v in pairs(Map:GetChildren()) do
        v:Destroy()
    end

    for i, v in pairs(game.Workspace:GetChildren()) do
        print(v)
        if v.ClassName ~= "Camera" and v.ClassName ~= "Terrain" and not v:FindFirstChildOfClass("Humanoid") then
            v:Clone().Parent = Map
        end
    end
end)
0
Thanks, It lags but ok. FixRobloxz 61 — 4y
0
it lags because you're running this every frame lol. all i did was fix your problem not improve your script. Godlydeathdragon 227 — 4y
Ad

Answer this question