Viewport not working

Hello,
I have a script:

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Values = ReplicatedStorage:WaitForChild('Values')
local Speaker = Values:WaitForChild('Speaker')


local ViewportCamera = Instance.new('Camera',script)
ViewportCamera.CameraType = Enum.CameraType.Scriptable

local ViewportPoint = Vector3.new(0,0,0)

local ViewportFrame = script.Parent

ViewportFrame.CurrentCamera = ViewportCamera

Speaker:GetPropertyChangedSignal('Value'):Connect(function()
	
	print('new')
	local Item = workspace:WaitForChild(Speaker.Value):Clone()
	
	Item.HumanoidRootPart.CFrame = CFrame.new(ViewportPoint)
	
	Item.Parent = ViewportFrame
	
	local cfrane,size = Item:GetBoundingBox()
	
	local Max = math.max(size.X,size.Y,size.Z)
	
	local Distance = (Max/math.tan(math.rad(ViewportCamera.FieldOfView))) * 1.5
	
	local CurrentDistance = (Max/2) + Distance
	
	ViewportCamera.CFrame = CFrame.new(ViewportPoint + Vector3.new(0,0,CurrentDistance),ViewportPoint)
end)

But it doesn’t work!
It should put the players character in a viewport.
Error:

Thanks for any help!

Does the Item contain a humanoidrootpart or are you trying to refer to a players humanoidrp?

1 Like

I’m cloning the players character.

You can easily make a playeradded event in the server, pass a remote from there and do what you want to do like that.

--Script--
game.Players.PlayerAdded:Connect(function(plr)
game.ReplicatedStorage.Remote:FireClient(plr)
end)

--Localscript--
local plr = game.Players.LocalPlayer
local char = plr.CharacterAdded:Wait()
game.ReplicatedStorage.Remote.OnClientEvent:Connect(function()
-- what u wanna do when player joins
end)
1 Like

If you want all of the players in the game to be in a viewport, you would use a local script. Use :GetPlayers() and clone every player’s character to a viewport every frame using RenderStepped. At the beginning of each frame, use :ClearAllChildren() to clear all characters in the viewport. After that, clone each character to the viewport. Hope this helps.

1 Like