How to Get Avatar in ViewportFrame

I want the ViewportFrame to update every time the player’s avatar changes (example when they equip a new hat) If possible I would like it to be a local script but I’m not sure how that would work and maybe the avatar model could be like 5 studs in front of the frame (Idk, whatever y’all think) Thanks for any help!:honeybee:

1 Like

Try this localscript:
Also insert a WorldModel in your Viewportframe.

local viewportFrame = script.Parent
local PlayerCharacter = game:GetService("Players"):CreateHumanoidModelFromUserId(game.Players.LocalPlayer.UserId)
PlayerCharacter.Parent = viewportFrame.WorldModel

local viewportCamera = Instance.new("Camera")
script.Parent.CurrentCamera = viewportCamera
viewportCamera.Parent = viewportFrame
viewportCamera.CFrame = CFrame.new(PlayerCharacter.HumanoidRootPart.Position - Vector3.new(0, 0, 5)) * CFrame.Angles(0, math.rad(180), 0)
3 Likes

How would I make it update when a player equips a new avatar item?

You can use a loop to change the humanoid description.

while wait(5) do
	local HumanoidDescription = game:GetService("Players"):GetHumanoidDescriptionFromUserId(game.Players.LocalPlayer.UserId)
	PlayerCharacter:FindFirstChildOfClass("Humanoid"):ApplyDescription(HumanoidDescription)
end
1 Like

I mean if they equip items using the in-game avatar editor. Sry, I should’ve been more specific. Thanks so much for getting meh this far tho!

Ok, then you can clone the player character.

local Players = game:GetService("Players")
game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

local function CloneCharacter(char)
	char.Archivable = true
	local plrchar = char:Clone()
	char.Archivable = false
	return plrchar
end

local viewportFrame = script.Parent
local PlayerCharacter = CloneCharacter(character)
PlayerCharacter.Parent = viewportFrame.WorldModel

local viewportCamera = Instance.new("Camera")
script.Parent.CurrentCamera = viewportCamera
viewportCamera.Parent = viewportFrame
viewportCamera.CFrame = CFrame.new(PlayerCharacter.HumanoidRootPart.Position - Vector3.new(0, 0, 5)) * CFrame.Angles(0, math.rad(180), 0)

while wait(5) do
	viewportFrame.WorldModel:ClearAllChildren()
	local PlayerCharacter = CloneCharacter(character)
	PlayerCharacter.Parent = viewportFrame.WorldModel
end
6 Likes

Sorry but how do I make the cloned character display animation in realtime?

(2 years later oh my god)

It is shocking you asked 2 years later but you can just create an animation inside the cloned character that is inside the viewport frame and have the cloned character play it on a loop, it should give you what you’re looking for.

1 Like

I didn’t even bother to fix this problem after 2 months, after you answered I went to studio and code it and it worked. (Did not know it was that simple sorry)

1 Like

I think to make animation play in real time you would have to parent to clone character to the ViewportFrame and not the WorldModel :slight_smile:

what about player watching to mouse and changes direction like character selection of some games
(like shop in surf)

For your use case you could hook an event for UserInputService.InputChanged that fires when the user’s way of interacting changes, but in this case it’s useful when Mouse.Position does. What I did in the code shown is to divide the Mouse.Position by the ViewportSize in the X axis. The viewport size is basically the size of the screen expressed in the Vector2 unit of measurement. And since it goes from 0 to 1 and we want its rotation to be negative as well, I added an offset of -0.5. Which by multiplying it by the maximum rotation in the RY axis and then multiplying it again by two, the expected result would be achieved.

UserInputService.InputChanged:Connect(function(input, gPE)
	local ViewportSize = Camera.ViewportSize
	local MouseLocation = UserInputService:GetMouseLocation()
	local MaximumRot = 45
	local Delta = MouseLocation.X/ViewportSize.X
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		PlayerCharacter:PivotTo(CFrame.new(PlayerCharacter:GetPivot().Position) * CFrame.Angles(0, math.rad((Delta-.5)*MaximumRot*2), 0))
	end
end)

I will try that and tell you if it works

1 Like