Help with Viewport Frame script

So, my viewport frame script has 2 problems. 1, its erroring out at line 15 Players.GoodGuy21938.PlayerGui.MainGui.Units.Units.Unit.ViewportFrame.Anim:15: invalid argument #2 (string expected, got nil). 2, Its making the game really laggy. original fps is 60, drops down to around 30-50.
If you have the solution to any of these problems, tell me.

run.RenderStepped:Connect(function()
	if game.Workspace.ViewportFrames[tower] then
		local children = game.Workspace.ViewportFrames[tower]:Clone()
		for i,v in pairs(children:GetChildren()) do
			if v:IsA("Part") or v:IsA("MeshPart") then
				v.Anchored = true
			end
		end
		children.Parent = script.Parent
		debris:AddItem(children, 0.1)
	end
end)

You could try this:

game:GetService("RunService").PreRender:Connect(function() -- PreRender is the new RenderStepped.

if game.Workspace.ViewportFrames[tower] then

for _, child in pairs(game.Workspace.ViewportFrames[tower]:GetDescendants()) do

if child:IsA("BasePart") then -- this checks for Parts and MeshParts.

local childClone = child:Clone()

childClone.Anchored = true

childClone.Parent = script.Parent

debris:AddItem(childClone , 0.1)

end

end

end

end)

Can you send the whole script? Line 15 in the script you provided doesn’t even exist

oh yeah. sorry.

repeat
	wait()
until game:IsLoaded()
local run = game:GetService("RunService")
local debris = game:GetService("Debris")
local tower = nil
if script.Parent.Parent.Name ~= "Unit" then
	tower = script.Parent.Parent.Name
	local anim = game.Workspace.ViewportFrames[tower].Humanoid:LoadAnimation(game.Workspace.ViewportFrames[tower].Animations.Idle)
	anim.Looped = true
	anim:Play()
end

run.RenderStepped:Connect(function()
	if game.Workspace.ViewportFrames[tower] then
		local children = game.Workspace.ViewportFrames[tower]:Clone()
		for i,v in pairs(children:GetChildren()) do
			if v:IsA("Part") or v:IsA("MeshPart") then
				v.Anchored = true
			end
		end
		children.Parent = script.Parent
		debris:AddItem(children, 0.1)
	end
end)

It still seems to error the same error.

The tower would stay nil if script.Parent.Parent.Name is Unit, creating the error. You can do a simple check if tower exists (before line 15)
if not tower then return end

For the lag, why would you need to create a viewport frame every time? Just create it once

thats the thing. The animation breaks when I dont run it every second and I have no idea on how to fix it.

Try using Heartbeat instead? RenderStepped can cause significant performance issues if not used on camera or character

it performs slightly better, but not the 60 fps.