Hello,
I’m trying to get a clone of the local player to walk in a loop inside of a viewport frame. The goal is to show off a trail accessory in a gui before the player decides to wear it. The clone of the player shows up in the viewport frame just fine and its idle animations work.
The issue comes when I try to use Humanoid:MoveTo(). I’ve checked in the Explorer tab and the clone’s WalkToPoint gets updated, but the clone doesn’t move and MoveToFinished never fires, even after 8 seconds.
I’ve looked around on the DevForum and haven’t seen anything that has helped me fix this issue. I’m thinking that this is related to the fact that the humanoid is the child of a WorldModel inside of a Viewport inside of a GUI. Is there any way to get around this?
Here’s the script that handles the clone.
local View = script.Parent
local PlayersService = game:GetService("Players")
-- ensure that player character is loaded
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 clonePlayerCharacter(character)
character.Archivable = true
local clone = character:Clone()
character.Archivable = false
return clone
end
--check for camera and create new instance if needed if needed
local function validateVpCam()
local vpCamera = View:FindFirstChild("vpCamera")
if not vpCamera then
vpCamera = Instance.new("Camera")
vpCamera.Parent = View
View.CurrentCamera = vpCamera
end
return vpCamera
end
--check for world model and instance new if needed
local function validateWorldModel()
local worldModel = View:FindFirstChild("WorldModel")
if not worldModel then
worldModel = Instance.new("WorldModel")
worldModel.Parent = View
end
return worldModel
end
--look for old character model and remove if there
local function clearOldClone()
local oldChar = View.WorldModel:FindFirstChild("PlayerClone")
if oldChar then
oldChar:Destroy()
end
end
local function validatePath()
local path = {
A = Vector3.new(0,0,0),
B = Vector3.new(3,0,4),
C = Vector3.new(6,0,0),
D = Vector3.new(3,0,-4)
}
return path
end
-- make clone walk endlessly
local function cloneLoop(clone, path)
-- clone starts off at A, so move to B
print(path)
print("trying to move to B...")
clone.Humanoid:MoveTo(path.B)
clone.Humanoid.MoveToFinished:Wait()
print("trying to move to C...")
clone.Humanoid:MoveTo(path.C)
clone.Humanoid.MoveToFinished:Wait()
print("trying to move to D...")
clone.Humanoid:MoveTo(path.D)
clone.Humanoid.MoveToFinished:Wait()
print("trying to move to A...")
clone.Humanoid:MoveTo(path.A)
clone.Humanoid.MoveToFinished:Wait()
end
-- main function that drives the viewport display
local function updateCharacterViewport()
local vpCamera = validateVpCam()
local worldModel = validateWorldModel()
clearOldClone()
local PlayerClone = clonePlayerCharacter(Character)
PlayerClone.Name = "PlayerClone"
PlayerClone.Parent = worldModel
PlayerClone.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
PlayerClone.HumanoidRootPart.Anchored = false
PlayerClone:PivotTo(CFrame.new(Vector3.new(0,0,0)))
--make camera face PlayerClone
vpCamera.CFrame = CFrame.new(PlayerClone.HumanoidRootPart.Position - Vector3.new(0, 0, 8)) * CFrame.Angles(0, math.rad(180), 0)
-- make PlayerClone walk endlessly to show off trail
local path = validatePath()
cloneLoop(PlayerClone, path)
end
updateCharacterViewport()