How to insert a model into a Viewport

Oops, sorry, forgot to commit, hold on

What on Earth is .p may I ask? I’m guessing it’s short for .Position

The line you provided is currently erroring, here is the modified code

game.Players:CreateHumanoidModelFromUserId(113260028).Parent = script.Parent.ViewportFrame
local model = script.Parent.ViewportFrame:WaitForChild("Player")
model.Name = " "
model.PrimaryPart.Position = Vector3.new(0, 0, 0)

local camera = Instance.new("Camera")
script.Parent.ViewportFrame.CurrentCamera = camera
camera.Parent = script.Parent.ViewportFrame

camera.CFrame = model.PrimaryPart.Position

Errors:

  23:52:07.697  Workspace.Face.SurfaceGui.Script:10: invalid argument #3 (CFrame expected, got Vector3)  -  Server - Script:10
  23:52:07.697  Stack Begin  -  Studio
  23:52:07.697  Script 'Workspace.Face.SurfaceGui.Script', Line 10  -  Studio - Script:10
  23:52:07.697  Stack End  -  Studio
  23:52:07.697  0  -  Server - Script:13
  23:52:07.764  Workspace.Face.SurfaceGui.ViewportFrame.Script:4: invalid argument #3 (Vector3 expected, got CFrame)  -  Server - Script:4
  23:52:07.764  Stack Begin  -  Studio
  23:52:07.764  Script 'Workspace.Face.SurfaceGui.ViewportFrame.Script', Line 4  -  Studio - Script:4
  23:52:07.764  Stack End  -  Studio

Here is a tree of the objects just incase this helps:
image

sorry for the late response, does the model have a primary part?

Yes, once it’s imported, it automatically assigns a PrimaryPart to the Head
image

Great, I didn’t see the error before, but now I do.

This should be:

camera.CFrame = model.PrimaryPart.CFrame

Still nothing sadly
image

Here is the new code:

game.Players:CreateHumanoidModelFromUserId(113260028).Parent = script.Parent.ViewportFrame
local model = script.Parent.ViewportFrame:WaitForChild("Player")
for _, v in pairs(model:GetDescendants()) do
	if v:IsA("Part") then
		v.Anchored = true
	end
end
model.Name = " "
model.PrimaryPart.Position = Vector3.new(0, 0, 0)

local camera = Instance.new("Camera")
script.Parent.ViewportFrame.CurrentCamera = camera
camera.Parent = script.Parent.ViewportFrame

camera.CFrame = model.PrimaryPart.CFrame

I had simillar issue with my game however I kinda managed to fixed it with a new camera Vector ( Vector3.new(12,6,-4) also set camera as scriptable

local camOffset = Vector3.new(12,6,-4)
local viewportCamera = Instance.new("Camera")
viewportCamera.CameraType = Enum.CameraType.Scriptable
viewportCamera.Parent = 
viewportCamera.CFrame = CFrame.new(g.HumanoidRootPart.Position + camOffset, g.HumanoidRootPart.Position))

I’m throwing in the towel, nothing works still, I’ll upload the model file, if anybody can help I’d be thankful
Model.rbxm (5.7 KB)

So I’ll tell you this.

local mcf = model.PrimaryPart.CFrame -- character's cframe
local center = mcf.Position + mcf.LookVector*10 -- 10 studs from the character
local lookingAt = mcf.Position
camera.CFrame = CFrame.lookAt(center, lookingAt)

^^ This will tell the camera to position 10 studs in front of the primary part, and look at the primaryPart. If you want it to look at the head or the arm or whatever, just change lookingAt to the head’s position or the arm’s position or whatever.

[Note] CFrame.lookAt is the same as CFrame.new except lookAt takes a 3rd arguement which is the UpVector. UpVector is (0,1,0) by default. You could change this and make the Camera tilt. It’s pretty neat.

This might be the issue, the way you spawing you player model makes it all scattered around in the Frame so you didn’t see it but on the actual gui you can see some of the items.

This still doesn’t seem to fix my issue, maybe I’m dumb but I cannot figure this out at all

That’s strangle, the method that was used in the script is the one that doesn’t break, why has it suddenly just screwed itself up again
Context: Load Character by UserId - #13 by Niestrat99

Update: This is really strange, it works completely fine in the command bar using game.Players:CreateHumanoidModelFromUserId(113260028).Parent = game.Workspace

So there’s this thing called a client-server model. And how that translates into Roblox experiences is that "LocalScript"s only run on client machines and "Script"s only run on the server. I got the impression that what you are doing is entirely client-based, but when I opened your model it was in a Script.

StarterPlayer.StarterPlayerScripts – used for LocalScript (s)
StarterGui – used for both LocalScript (s), ScreenGui (s), BillboardGui (s) if you want to, SurfaceGui (s) if you want to.

Now. You can place ViewportFrame (s) inside of ScreenGui (s), BillboardGui (s), and SurfaceGui (s). (I think you can at least.)

So my suggestion is to create a ScreenGui inside of StarterGui and then place a ViewportFrame inside of the ScreenGui. Place a LocalScript inside of the ViewportFrame and then edit the viewport using code.

I don’t want it to be on the screen, what my goal is to make a small leaderboard that the top player is shown in a Viewport that slowly rotates
I understand the client-server networking model but, I don’t understand why the server cannot handle viewports

Well let me add this then:

Camera – client-only; not replicated.

Hm, wait, yeah I should add a LocalScript to insert it’s own Camera into the Viewports, I feel dumb now
Thank you

Even client-side, it will not even display the body at all

EditedModel.rbxm (6.5 KB)
So it took me a while because I’ve never worked with ViewportFrame ever. But after researching I’ve come to a better understanding. For this model, place the LocalScript in StarterPlayer.StarterPlayerScripts, place Face in Workspace, and place SurfaceGui in StarterGui.

In my experience, I set the Adornee of the SurfaceGui to Face, placed a wait() after the character construct function call, moved everything to a local script, and placed SurfaceGui into StarterGui.

[1st edit] I’ve no idea why you can’t just leave the SurfaceGui in the Face part and leave Adornee blank.

[2nd edit] I don’t think ViewportFrame works if it is not in PlayerGui. Therefore you must put SurfaceGui in PlayerGui (game.Players.LocalPlayer.PlayerGui (which StarterGui will do for you)), and then set the Adornee of the SurfaceGui to the part. But only if it has a ViewportFrame.

2 Likes

I can’t thank you enough, thank you very much

While doing a bit of tinkering, I believe it would be a good idea to load the avatar into Workspace first, then move it back into the Viewport because for some reason all of the accessories and attachments mess up
E.g.

char.Parent = game.Workspace
wait(2)
char.Parent = vp

Without:


With:

1 Like