I was trying to make a health GUI, I’m trying to make a viewport frame camera lerp to a copy of the character, it says I got a the error "attempt to index nil with ‘Parent’ at line 18. Here’s my code
local frame = script.Parent:WaitForChild("ViewportFrame")
local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local cam = Instance.new("Camera")
frame.CurrentCamera = cam
cam.CameraType = Enum.CameraType.Scriptable
cam.Parent = script.Parent
cam.Name = "ViewportCamera"
repeat wait() until char.PrimaryPart
cam.CFrame = CFrame.new(char.PrimaryPart.Position + Vector3.new(-2,2,-2),char.PrimaryPart.Position)
local cameraSpeed = 0.6
game:GetService("RunService").RenderStepped:Connect(function()
local shownChar = char:Clone()
frame:ClearAllChildren()
shownChar.Parent = frame
repeat wait() until shownChar.PrimaryPart
cam.CFrame = cam.CFrame:Lerp(CFrame.new(shownChar.PrimaryPart.Position + Vector3.new(-2,2,-2),char.PrimaryPart.Position),cameraSpeed)
end)
To clone a character set the Character.Archivable to true and after cloning return it back to false, to prevent exploiters from being able to “break” it by a way or another