(Viewportframe) Player camera isn't following the player

I am trying to use the viewport frame. I got the viewport frame in the screen gui on the side of the screen. The issue I am having is that the camera view of the player is in the same spot view of the viewport frame.

game:GetService("Workspace")
local cam = workspace.CurrentCamera
local pic = workspace.pic
local view = script.Parent.ViewportFrame
game:GetService("RunService").Heartbeat:Connect(function(run)
    cam.CFrame = pic.CFrame
cam.CameraType = Enum.CameraType.Scriptable
cam.FieldOfView = 70
cam.CameraSubject = pic
view.CurrentCamera = cam
for i, child in pairs(workspace:GetChildren()) do
if not child:IsA("Terrain") and not child:IsA("Camera") and child ~= pic then
child.Archivable = true
local clone = child:Clone()
clone.Parent = view
end
end
end)
2 Likes

Anyone have an idea what i did wrong here??

Use worldmodel to display models in viewportframes

1 Like

CameraType = Custom

when using CameraType as Scriptable you will have to update the CFrame manually or thats what i think it is just Try Setting the CameraType to Enum.CameraType.Custom

And set the CameraSubject to the part you want to follow

1 Like

If this helped you please consider marking my message as solution, Thanks.

1 Like

Hi, there thank you for the reply.
I changed the camera type to custom

Cam.CameraType = Enum.CameraType.Custom

What do you mean by setting the part I want to follow I think I already got it in the script. Is this part your meaning because pic is the part I want it to follow??

cam.CameraSubject = pic

Hi, there, Thanks for the reply!
But what is a worldmodel and how would I display models in viewportframe anyways??

1 Like

Also, changing Camera Type to Custom didn’t do anything differently.

It should work something like this.

local Players,Player = game:GetService("Players"),game:GetService("Players").LocalPlayer
local Viewport = Player.PlayerGui.vGui.ViewportFrame
local SteppedConnection 
function FollowCamera(Camera:Camera?, Humanoid:Humanoid?, hrp:BasePart?)
	Camera.CameraSubject = Humanoid
	Camera.CameraType = Enum.CameraType.Scriptable
	SteppedConnection = Run.Stepped:Connect(function()
		Camera.CFrame = CFrame.lookAt(hrp.Position + Vector3.new(0, 0, 5), hrp.Position)
	end)
end
function CreateStuff()
	local DummyHumanoid = Players:CreateHumanoidModelFromUserId(Player.UserId)
	local vCamera = Instance.new("Camera")
	DummyHumanoid.Name = Player.Name
	Viewport.CurrentCamera = vCamera
	DummyHumanoid.Parent = Viewport.WorldModel
	vCamera.Parent = Viewport
	FollowCamera(vCamera, DummyHumanoid.Humanoid, DummyHumanoid.PrimaryPart)
end
function Cleanup()
	SteppedConnection:Disconnect()
	for _,v in Viewport:GetDescendants() do
		if not v:IsA("WorldModel") then
			v:Destroy()
		end
	end
end
CreateStuff()
1 Like
local workspace = game:GetService("Workspace")
local pic = workspace.pic
local view = script.Parent.ViewportFrame

-- Create a new camera for the viewport frame
local viewportCamera = Instance.new("Camera")
viewportCamera.Parent = view
view.CurrentCamera = viewportCamera

game:GetService("RunService").Heartbeat:Connect(function(run)
    -- Set the viewport camera to look at the 'pic' object
    viewportCamera.CFrame = pic.CFrame

    for i, child in pairs(workspace:GetChildren()) do
        if not child:IsA("Terrain") and not child:IsA("Camera") and child ~= pic then
            child.Archivable = true
            local clone = child:Clone()
            clone.Parent = view
        end
    end
end)

1 Like

I keep getting errors on this line

DummyHumanoid.Parent = Viewport.WorldModel

also run keeps getting underlined in blue

SteppedConnection = Run.Stepped:Connect(function()

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.