Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I clone the player's character and have it appear on my ViewportFrame?

Asked by 5 years ago

So basically I want to clone the player's character upon joining the game and have it appear on the ViewportFrame. Currently I'm getting no errors and I simply think it's because I made a mistake while trying to clone the player's character. Any help would be appreciated as always, thanks!

--Local Script
local player = game.Players.LocalPlayer
local character = game.Players.LocalPlayer.Character
local viewportFrame = script.Parent


if player.CharacterAdded and player:FindFirstChild("Humanoid")
then character.Archivable = true
    local clone = player.Character:Clone()
    clone.Position = Vector3.new(0,0,0)
    clone.Parent = viewportFrame

    local viewportCamera = Instance.new("Camera")
    viewportFrame.CurrentCamera = viewportCamera
    viewportCamera.Parent = viewportFrame
    viewportCamera.CFrame = CFrame.new(Vector3.new(0,2,12),clone.position)
end

1 answer

Log in to vote
0
Answered by 5 years ago

Alright, so there's multiple problems with your script...

Your conditional statement (if-then) is a bit incorrect. I don't think a CharacterAdded event would be a good idea to use in the statement, and you're searching for the humanoid in the player itself. The humanoid is in the character, not the player.

Then an error occurs. On line 9, clone is a model (player characters are models), and it doesn't have a position property. Instead, you should use the :MoveTo() function. It's pretty much the position property in the form of a function for models.

After that, another error occurs at line 16, and it's similar to the last error. clone doesn't have a position property, but :MoveTo() doesn't let you read the position, only change it. There's several ways to get the position, but I'm just going to get the HumanoidRootPart's position in the character.

Your script should look like this:

--Local Script
local player = game.Players.LocalPlayer
local character = game.Players.LocalPlayer.Character
local viewportFrame = script.Parent

repeat wait(1) until character ~= nil -- Waits for character to load

if character:FindFirstChild("Humanoid") then
    character.Archivable = true
    local clone = player.Character:Clone()
    clone:MoveTo(Vector3.new(0, 0, 0))
    clone.Parent = viewportFrame

    local viewportCamera = Instance.new("Camera")
    viewportFrame.CurrentCamera = viewportCamera
    viewportCamera.Parent = viewportFrame
    viewportCamera.CFrame = CFrame.new(Vector3.new(0,2,12),clone.HumanoidRootPart.Position)
end

Hope this helped! If you have any questions, just comment!

1
Instead I removed the if statement all together and used my variables, making character = player.Character or player.CharacterAdded:Wait(). I also added another variable, local humanoid = character:WaitForChild("Humanoid"). Although, I couldn't figure out how to reference the position of the model. Really appreciate your help, thanks! teeth3444444444 0 — 5y
0
^ There's some properties and functions in the model that might help you reference its position, like BasePart or :GetPrimaryPartCFrame(). You can see them here : https://developer.roblox.com/api-reference/class/Model User#20279 0 — 5y
Ad

Answer this question