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

Why is getchildren index nil?

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.

2 comments

0

Where is the SurfaceGui located?

0

StarterGui

1 answer

✓ Accepted answer

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)

2 comments

0

Thanks, It lags but ok.

0

it lags because you're running this every frame lol. all i did was fix your problem not improve your script.

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